-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinCapTransportation.cpp
More file actions
59 lines (54 loc) · 912 Bytes
/
MinCapTransportation.cpp
File metadata and controls
59 lines (54 loc) · 912 Bytes
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
#include <iostream>
using namespace std;
int g[100];
int maxrides(long long s,int n)
{
int rides=1;
long long sum=0;
for(int i=0;i<n;i++)
{
sum+=g[i];
if(sum>s)
{
rides++;
sum=0;
i--;
}
}
return rides;
}
int mincap(int n,int k)
{
int cur,ma=g[0];
long long tot=0,mid,ans=-1;
for(int i=0;i<n;i++)
{
tot+=g[i];
if(ma<g[i])
ma=g[i];
}
long long l=ma;
long long h=tot;
while(l<=h)
{
mid=(h+l)/2;
cur=maxrides(mid,n);
if(cur<=k)
{
ans=mid;
h=mid-1;
}
else
l=mid+1;
}
return ans;
}
int main()
{
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++)
cin>>g[i];
cout<<mincap(n,k);
return 0;
}