CodeForces - 670D2 Magic Powder - 2 (二分&模拟)

CodeForces - 670D2

Magic Powder - 2

Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Submit
Status

Description

The term of this problem is the same as the previous one, the only exception — increased restrictions.

Input

The first line contains two positive integers n and
k (1?≤?n?≤?100?000,?1?≤?k?≤?109) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1,?a2,?...,?an (1?≤?ai?≤?109),
where the i-th number is equal to the number of grams of the
i-th ingredient, needed to bake one cookie.

The third line contains the sequence b1,?b2,?...,?bn (1?≤?bi?≤?109),
where the i-th number is equal to the number of grams of the
i-th ingredient, which Apollinaria has.

Output

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

Sample Input

Input

1 1000000000
1
1000000000

Output

2000000000

Input

10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1 1 1 1 1

Output

0

Input

3 1
2 1 4
11 3 16

Output

4

Input

4 3
4 3 5 6
11 12 14 20

Output

3

Sample Output

Hint

Source

Codeforces Round #350 (Div. 2)

//题意:同上一题,就是数开大了,得用二分

Hait: INF比较小,所以得用3000000000ll就行了

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define ll long long
#define INF 0x3f3f3f3f
#define N 100010
using namespace std;
ll a[N],b[N];
int n;
ll k;
bool judge(ll x)
{
	ll s=k;
	for(int i=1;i<=n;i++)
	{
		if(x*a[i]>=b[i])
			s-=(x*a[i]-b[i]);
		if(s<0)
			return false;
	}
	return true;
}
int main()
{
	int i;
	while(scanf("%d%lld",&n,&k)!=EOF)
	{
		for(i=1;i<=n;i++)
			scanf("%lld",&a[i]);
		for(i=1;i<=n;i++)
			scanf("%lld",&b[i]);
		if(n==1)
		{
			printf("%lld\n",(k+b[1])/a[1]);
			continue;
		}
		ll l=0,r=3000000000ll,mid,ans;
		while(l<=r)
		{
			mid=(l+r)>>1ll;
			if(judge(mid))
			{
				l=mid+1;
				ans=mid;
			}
			else
				r=mid-1;
		}
		printf("%lld\n",ans);
	}
	return 0;
}

这是刚开始写的,比较麻烦。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define ll long long
#define INF 0x3f3f3f3f
#define N 100010
using namespace std;
ll a[N],b[N];
struct zz
{
	ll a;
	ll b;
	ll c;
	ll d;
}p[N];
bool cmp(zz a,zz b)
{
	return a.a<b.a;
}
int n;
ll k;
bool judge(ll x)
{
	ll s=0;
	for(int i=1;i<=n;i++)
	{
		if(p[i].a<x)
			s+=p[i].c+(x-p[i].a-1)*p[i].d;
		if(s>k)
			return false;
	}
	return true;
}
int main()
{
	int i;
	while(scanf("%d%lld",&n,&k)!=EOF)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%lld",&a[i]);
			p[i].d=a[i];
		}
		for(i=1;i<=n;i++)
			scanf("%lld",&b[i]);
		if(n==1)
		{
			printf("%lld\n",(k+b[1])/a[1]);
			continue;
		}
		for(i=1;i<=n;i++)
		{
			p[i].a=b[i]/a[i];
			p[i].b=b[i]%a[i];
			p[i].c=a[i]-p[i].b;
		}
		sort(p+1,p+n+1,cmp);
		ll l=0,r=3000000000ll,mid,ans;
		while(l<=r)
		{
			mid=(l+r)>>1ll;
			if(judge(mid))
			{
				l=mid+1;
				ans=mid;
			}
			else
				r=mid-1;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
时间: 2024-10-09 09:18:31

CodeForces - 670D2 Magic Powder - 2 (二分&模拟)的相关文章

Codeforces 670D2 Magic Powder - 2 二分答案

Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai - how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinar

CodeForces - 670D1 Magic Powder - 1 (模拟)

CodeForces - 670D1 Magic Powder - 1 Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description This problem is given in two versions that differ only by constraints. If you can solve this problem in large c

codeforces 670D2 - Magic Powder - 2

和前面的那道题一样,就只改了数组的大小和数据类型. #include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 5; //int a[MAX], b[MAX], c[MAX]; typedef __int64 ll; struct NODE { ll a, b, c; }node[MAX]; ll sum[MAX]; bool comp(NODE a, NODE b) { return a.c < b.c; }

Codeforces 670D. Magic Powder

Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai - how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinar

codeforces 670D1 - Magic Powder - 1

题目的意思,就是给出制作一种食物的每种材料所需的量,然后再给出每种材料目前总共的数量,问最多可以制作多少个这样的食物. 贪心.首先求出每种材料是总共有多少个这样的材料,然后由小到大排序,然后再用一个数组存那个后一个大的量的材料减去前面所有小的量的差,因为比如,有3种材料,每种材料的量分别是1, 2,4, 就需要sum[1] = 1, sum[2] = 3,然后就是贪心的计算了. #include <bits/stdc++.h> using namespace std; const int MA

Magic Powder - 1 CodeForces - 670D1

This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you

Codeforces 374D Inna and Sequence 二分+树状数组

题目链接:点击打开链接 给定n个操作,m长的序列a 下面n个数 if(co>=0)则向字符串添加一个co (开始是空字符串) else 删除字符串中有a的下标的字符 直接在序列上搞,简单模拟 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h&

Magic Powder - 2 (CF 670_D)

http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous one, the only exception — increased restrictions. Input The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109) — the nu

Codeforces 97D Robot in Basement bitset+模拟

题目链接:点击打开链接 题意: 每个点有一个机器人(.),下面是一些指令,每次发出指令(一个字母)所有机器人都会执行移动. 当机器人到E点就会离开. 若机器人前方是'#' 或者边界则停留原地.一个方格可以站多个机器人. bitset模拟.. #include <cstdio> #include <cstring> #include <algorithm> #include <bitset> using namespace std; char s[100005