标签:
Description
Chef is the head of commercial logging industry that recently bought a farm containing N trees. You are given initial height of the i-th tree by Hi and the rate of growth of height as Ri meters per month. For simplicity, you can assume that all the trees are perfect cylinders of equal radius. This allows us to consider only the height of trees when we talk about the amount of wood.
In Chef‘s country, laws don‘t allow one to cut a tree partially, so one has to cut the tree completely for gathering wood. Also, laws prohibit cutting trees of heights (strictly) lower than L meters.
Today Chef received an order of W meters (of height) of wood. Chef wants to deliver this order as soon as possible. Find out how minimum number of months he should wait after which he will able to fulfill the order. You can assume that Chef‘s company‘s sawing machines are very efficient and take negligible amount of time to cut the trees.
There is a single test case per test file.
The first line of the input contains three space separated integers N, W and L denoting the number of trees in the farm, the amount of wood (in meters) that have to be gathered and the minimum allowed height of the tree to cut.
Each of next N lines contain two space separated integers denoting Hi and Ri respectively.
Output a single integer denoting the number of months that have to pass before Chef will be able to fulfill the order.
Input: 3 74 51 2 2 5 7 2 9 Output: 7
After 6 months, heights of each tree will be 14, 47 and 56 respectively. Chef is allowed to cut only the third tree, sadly it is not enough to fulfill an order of 74 meters of wood.
After 7 months, heights of each tree will be 16, 54 and 65 respectively. Now Chef is allowed to cut second and third trees. Cutting both of them would provide him 119 meters of wood, which is enough to fulfill the order.
Input
Output
Sample Input
Sample Output
Hint
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #define ll long long 5 using namespace std; 6 ll n,w,l; 7 struct node 8 { 9 ll h,r; 10 }N[100005]; 11 bool check(ll exm) 12 { 13 ll sum=0; 14 if(exm==0) 15 { 16 for(int i=1;i<=n;i++) 17 { 18 if(N[i].h>=l) 19 sum+=N[i].h; 20 if(sum>=w) 21 { 22 return true; 23 } 24 } 25 return false; 26 } 27 for(int i=1;i<=n;i++) 28 { 29 if((N[i].r>(l-N[i].h)/exm)||(N[i].r==(l-N[i].h)/exm&&(l-N[i].h)%exm==0))//********* 30 { 31 sum=sum+N[i].h; 32 ll cha=w-sum; 33 if(sum>=w) 34 { 35 return true; 36 } 37 if(cha/N[i].r<=exm) 38 { 39 return true; 40 } 41 sum=sum+exm*N[i].r; 42 } 43 } 44 return false; 45 } 46 int main() 47 { 48 while(scanf("%I64d %I64d %I64d",&n,&w,&l)!=EOF) 49 { 50 memset(N,0,sizeof(N)); 51 for(int i=1;i<=n;i++) 52 scanf("%I64d %I64d",&N[i].h,&N[i].r); 53 ll l=0; 54 ll r=w; 55 ll mid; 56 while(l<r) 57 { 58 mid=(l+r)>>1; 59 if(check(mid)) 60 r=mid; 61 else 62 l=mid+1; 63 } 64 cout<<l<<endl; 65 } 66 return 0; 67 }
codechef May Challenge 2016 FORESTGA: Forest Gathering 二分
标签:
原文地址:http://www.cnblogs.com/hsd-/p/5667335.html