简单二分
Posted h404nofound
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单二分相关的知识,希望对你有一定的参考价值。
The term of this problem is the same as the previous one, the only exception — increased restrictions.
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.
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
1 1000000000
1
1000000000
2000000000
10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1 1 1 1 1
0
3 1
2 1 4
11 3 16
4
4 3
4 3 5 6
11 12 14 20
3
1 #include<cstdio> 2 using namespace std; 3 typedef long long ll; 4 const ll MAXN=2*1e9+5; 5 ll n, k;int i; 6 ll a[100005],b[100005]; 7 ll binarySearch(ll l,ll r){ 8 ll mid,sum; 9 while(l <= r){ 10 mid = (l+r)/2,sum = 0; 11 for( i = 0; i < n; i++){ 12 if(b[i] < mid*a[i])sum += (mid*a[i]-b[i]); 13 if(sum > k)break; 14 } 15 if(sum==k){ 16 return mid; 17 } 18 else if(sum < k)l = mid+1; 19 else r = mid-1; 20 } 21 return l-1; 22 } 23 int main() 24 { 25 scanf("%d %d", &n, &k); 26 for( i = 0; i < n; i++) scanf("%d", &a[i]); 27 for( i = 0; i < n; i++) scanf("%d", &b[i]); 28 printf("%lld",binarySearch( 0 , MAXN)); 29 return 0; 30 }
Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters ‘B‘ (bread), ‘S‘ (sausage) и ‘C‘ (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "ВSCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again.
Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese.
Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient.
The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn‘t exceed 100, the string contains only letters ‘B‘ (uppercase English B), ‘S‘ (uppercase English S) and ‘C‘ (uppercase English C).
The second line contains three integers nb, ns, nc (1 ≤ nb, ns, nc ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus‘ kitchen. The third line contains three integers pb, ps, pc (1 ≤ pb, ps, pc ≤ 100) — the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≤ r ≤ 1012) — the number of rubles Polycarpus has.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Print the maximum number of hamburgers Polycarpus can make. If he can‘t make any hamburger, print 0.
BBBSSC
6 4 1
1 2 3
4
2
BBC
1 10 1
1 10 1
21
7
BSC
1 1 1
1 1 3
1000000000000
200000000001
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 typedef long long ll; 5 const ll MAXN = 2*1e12+5; 6 int a[5],b[5],c[5],len; 7 char s[1000]; 8 ll ru;int i,j; 9 ll bs(ll l,ll r){ 10 11 while(l <= r){ 12 ll mid = (l+r)/2,sum = 0; 13 for( j = 0; j < 3; j++){ 14 if(b[j] < a[j]*mid) sum += c[j]*(a[j]*mid-b[j]); 15 if(sum > ru)break; 16 } 17 if(sum == ru)return mid; 18 else if(sum < ru)l = mid+1; 19 else if(sum > ru)r = mid-1; 20 } 21 return l-1; 22 } 23 int main () 24 { 25 memset(a,0,sizeof(a)); 26 scanf("%s",s); 27 len=strlen(s); 28 for(i = 0;i < len; i++){ 29 if(s[i]==‘B‘)a[0]++; 30 else if(s[i]==‘S‘)a[1]++; 31 else if(s[i]==‘C‘)a[2]++; 32 } 33 scanf("%d %d %d", &b[0], &b[1], &b[2]); 34 scanf("%d %d %d", &c[0], &c[1], &c[2]); 35 scanf("%lld", &ru); 36 printf("%lld", bs(0,MAXN)); 37 return 0; 38 }
以上是关于简单二分的主要内容,如果未能解决你的问题,请参考以下文章