码迷,mamicode.com
首页 > 其他好文 > 详细

codechef May Challenge 2016 FORESTGA: Forest Gathering 二分

时间:2016-07-13 19:46:01      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

Description

All submissions for this problem are available.

Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.

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.

Input

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

Output a single integer denoting the number of months that have to pass before Chef will be able to fulfill the order.

Constraints

  • 1N105
  • 1W, L1018
  • 1Hi, Ri109

Subtasks

  • Subtask #1 [40 points]: 1 ≤ N, W, L104
  • Subtask #2 [60 points]: No additional constraints

Example

Input:
3 74 51
2 2
5 7
2 9

Output:
7

Explanation

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

Source Limit: 50000
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.3.2, CPP 4.9.2, CPP14, CS2, D, ERL, FORT, FS, GO, HASK, ICK, ICON, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYPY, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM chicken, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC
 
题意:中文题面 https://s3.amazonaws.com/codechef_shared/download/translated/MAY16/mandarin/FORESTGA.pdf
题解:二分处理 二分月数 但是这里的check 需要特殊处理 直接计算相乘会爆LL
        技巧除法处理 注意代码中的*位置 除的时候要注意考虑细节问题
 
 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!