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;
}