D - Squirrel and chestnut(二分)

D - Squirrel and chestnut
There are n squirrel(s) waiting below the feet of m chestnut tree(s). The first chestnut of the i-th tree will fall right after Ti second(s), and one more every Pi second(s) after that. The “big mama” of squirrels wants them to bring their nest no less than k chestnuts to avoid the big storm coming, as fast as possible! So they are discussing to wait below which trees to take enough chestnuts in the shortest time. Time to move to the positions is zero, and the squirrels move nowhere after that.

Request

Calculate the shortest time (how many seconds more) the squirrels can take enough chestnuts.
Input

The first line contains an integer t, the number of test cases, for each:
The first line contains the integers m,n,k, respectively.
The second line contains the integers Ti (i=1..m), respectively.
The third line contains the integers Pi (i=1..m), respectively.
(Each integer on a same line is separated by at least one space character, there is no added line between test cases)
Output

For each test cases, output in a single line an integer which is the shortest time calculated.

Example

Input:
2
3 2 5
5 1 2
1 2 1
3 2 5
5 1 2
1 1 1
Output:
4
3
* Explain (case #1): 2 squirrels waiting below the trees 2 and 3.

Limitations

0

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define MAX 99999
using namespace std;
long long int a[MAX], b[MAX];
long long int c[MAX];
int n, m, k;
long long int maxm(long long int a, long long int b)//比较大小
{
    return a>b?a:b;
}
long long int cmp(long long int a, long long int b)
{
    return a>b;
}
int check(long long int mid)//看看在mid的时间,能不能满足要求
{
    for(int i=0;i<m;i++)
    {
        if(mid<a[i])
            c[i] = 0;//此时没有果子
        else
        {
            c[i] = maxm(0, (mid-a[i]+b[i])/b[i]);//此时,果子数目
        }
    }
    sort(c, c+m, cmp);//按照果子数目由多到少排序
    long long int ans = 0;
    for(int i=0;i<min(n, m);i++)//现在最多可以得到多少的果子
    {
        ans += c[i];
        if(ans>=k)//满足要求,返回1
            return 1;
    }
    return 0;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d %d", &m, &n, &k);
        for(int i=0;i<m;i++)
            scanf("%lld", a+i);
        for(int i=0;i<m;i++)
            scanf("%lld", b+i);
        long long int l = 1;
        long long int r = MAX;
        long long int s = -1;
        while(l<=r)//对时间进行二分
        {
            long long int mid = (l + r) / 2;
            if(check(mid))
            {
                r = mid - 1;
                s = mid;
            }
            else
                l = mid + 1;
        }
        printf("%lld\n", s);//输出合适的时间
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值