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 Pisecond(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.
Calculate the shortest time (how many seconds more) the squirrels can take enough chestnuts.
For each test cases, output in a single line an integer which is the shortest time calculated.
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.
#include <bits/stdc++.h>
using namespace std;
int T[10005];
int P[10005];
int num[10005];
int k,m,n;
bool cmp(int a,int b)
{
return a>b;
}
int ok(int t)
{
int i,j;
int sum = 0;
for(i=1;i<=m;i++)
{
if(T[i] > t){num[i] = 0;continue;}
num[i] = (t-T[i])/P[i]+1;
if(num[i] > k) return 1;
}
sort(num+1,num+1+m,cmp);
sum = 0;
for(i=1;i<=n&&i<=m;i++)
{
sum += num[i];
if(sum >= k) return 1;
}
return 0;
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&m,&n,&k);
for(i=1;i<=m;i++)
{
scanf("%d",&T[i]);
}
for(i=1;i<=m;i++)
{
scanf("%d",&P[i]);
}
int l,r,mid;
l = 0;
r = 2e9;
while(l+1>r)
{
mid = (l+r)>>1;
if(ok(m)) r = m;
else l = m;
}
if(ok(l)) printf("%d \n",l);
else printf("%d \n",r);
}
return 0;
}