MAIN8_C - Shake Shake Shaky
no tags
Shaky has N (1<=N<=50000) candy boxes each of them contains a non-zero number of candies (between 1 and 1000000000). Shakey want to distibute these candies
among his K (1<=K<=1000000000) IIIT-Delhi students. He want to distibute them in a way such that:
Shaky has N (1<=N<=50000) candy boxes each of them contains a non-zero number of candies (between 1 and 1000000000). Shakey want to distibute these candies among his K (1<=K<=1000000000) IIIT-Delhi students. He want to distibute them in a way such that:
1. All students get equal number of candies.
2. All the candies which a student get must be from a single box only.
As he want to make all of them happy so he want to give as many candies as possible. Help Shaky in finding out what is the maximum number of candies which a student can get.
Input
First line contains 1<=T<=20 the number of test cases. Then T test cases follow. First line of each test case contains N and K. Next line contains N integers, ith of which is the number of candies in ith box.
Output
For each test case print the required answer in a seperate line.
Example
Input: 2 3 2 3 1 4 4 1 3 2 3 9 Output: 3 9
#include<iostream>
#include<algorithm>
//#include<bits/stdc++.h>
long long int arr[1000000];
using namespace std;
int main()
{
long long int t;
cin>>t;
while(t--)
{
long long int box,stud;
cin>>box>>stud;
for(long long int i=0;i<box;i++)
cin>>arr[i];
sort(arr,arr+box);
long long int lo=arr[box-1]/stud;
long long int ans=lo;
long long int hi = arr[box-1];
if(lo==0 && hi==1)
{
cout<<"1"<<endl;
continue;
}
// cout<<"initial lo "<<lo<<" hi "<<hi<<endl;
while(lo<=hi)
{
long long int mid=(lo+hi)/2;
// cout<<"mid "<<mid<<endl;
long long int stud_covered=0;
for(long long int i=0;i<box;i++)
{
if(mid!=0)
stud_covered+=arr[i]/mid;
}
if(stud_covered>=stud)
{
lo=mid+1;
if(mid>ans) ans=mid;
}
else
{
hi=mid-1;
}
}
cout<<ans<<endl;
}
return 0;
}
No comments:
Post a Comment