CodeChef - FORESTGA-二分题

Forest Gathering Problem Code: FORESTGA

All submissions for this problem are available.

Read problems statements in Mandarin ChineseRussian and Vietnamese as well.

Chef is the head of commercial logging industry that recently bought a farm containing Ntrees. 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 NW 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 Rirespectively.

Output

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

Constraints

  • 1 ≤ N ≤ 105
  • 1 ≤ WL ≤ 1018
  • 1 ≤ HiRi ≤ 109

Subtasks

  • Subtask #1 [40 points]: 1 ≤ NWL ≤ 104
  • 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 1447 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 1654 and 65 respectively. Now Chef is allowed to cut second and third trees. Cutting both of them would provide him 119meters of wood, which is enough to fulfill the order.

题意:给你n棵数,首先给你三个树,树的数量n,需要砍的长度w,树的最小砍伐高度l;

   n行,起始高度和每天增加长度;

思路:二分查找答案,防止爆long long 注意break;

代码
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 1e5+7;
ll h[maxn], r[maxn];
ll w,l,n;
bool check(ll x) {
    ll ans = 0;
    for(int i=0;i<n;i++) {
        ll temp = h[i] + r[i] * x;
        if(temp >= l) ans += temp;
        if(ans >= w) break;
    }
    return ans >= w;
}
int main()
{
    scanf("%lld%lld%lld", &n, &w, &l);
    for(int i = 0; i < n; i++)  {
            scanf("%lld%lld",&h[i], &r[i]);
    }
    ll l = 0, r = 1e18, mid,ans;
    while(l <= r){
        mid = (l + r) >> 1;
        if(check(mid)) ans = mid,r = mid - 1;
        else l = mid + 1;
    }
    printf("%lld\n",ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值