-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRATA.cpp
66 lines (56 loc) · 1.29 KB
/
PRATA.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// https://www.spoj.com/problems/PRATA/
/*
This problem uses binary search approach , as answer is monotonic function . ( of true and false on time ).
By applying binary search on time , we get the answer.
*/
#include <iostream>
using namespace std;
bool isPossible(int p,int cook[],int n,int mid){
int cnt = 0;
for(int i=0;i<n;i++){
//for each cook count the pratas
int c = 0;
int time = mid;
int perpratas = cook[i]; // time taken to cook each pratas by ith cook
while(time > 0){
time = time - perpratas;
if( time >= 0){
c += 1;
perpratas += cook[i];
}
}
cnt += c;
if(cnt >= p)
return true;
}
return false;
}
int minTime(int p,int cook[],int n){
int s = 0, e = 10000007;
int mid,ans=-1;
while(s<=e){
mid = (s+e)/2;
if(isPossible(p,cook,n,mid)){
ans = mid;
e = mid - 1;
}else{
s = mid + 1;
}
}
return ans;
}
int main(){
int t;
cin>>t;
while(t--){
int p;
cin>>p;
int n;
cin>>n;
int cook[n];
for(int i=0;i<n;i++)
cin>>cook[i];
cout<<minTime(p,cook,n)<<endl;
}
return 0;
}