-
Notifications
You must be signed in to change notification settings - Fork 0
/
prata.cpp
86 lines (74 loc) · 1 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//could not implement properly
#include<bits/stdc++.h>
using namespace std;
bool isCook(int ranks[],int p, int cooks,int mid)
{
int count=0;
for(int i=0;i<cooks;i++)
{
int time=0;
int j=1;
while(1)
{
time = time+ j*ranks[i];
j++;
if(time>mid)
{
break;
}
count++;
}
}
return (count >= p);
}
int findprata(int ranks[], int p, int cooks)
{
int maxx= INT_MIN;
for(int i=0;i<cooks;i++)
{
maxx = max(maxx,ranks[i]);
}
int i=1;
int time=0;
int count =0;
//find time by the cook for binary search
while(count<p)
{
time = time + i*maxx;
}
//binary search
int s=0;
int e=time;
while(s <= e)
{
int mid=(s+e)/2;
// int ans;
if(isCook(ranks,p,cooks,mid))
{
// ans = min(ans,mid);
e = mid;
}
else
s= mid;
}
return e;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int p;
cin>>p;
int cooks;
cin>>cooks;
int ranks[cooks];
for(int j=0;j<cooks;j++)
{
cin>>ranks[j];
}
cout<<findprata(ranks,p,cooks)<<endl;
}
return 0;
}