SSLOJ 1332 hard to buy gifts
Description–
Small X students to the students chose C N small gift, decided to buy and gift order, but as a poor child did not pay no pocket money, there are M-bit intentioned students lent a helping hand, however, in order to reduce the highest borrowing volume, small X students OI hope you contest his rational planning, so that he can be relaxed and happy to send gifts.
Input–
The first input line separated by a space of two positive integers N and M are
the following N lines each a positive integer not more than 10,000, the gift order Prices.
Output–
An integer that is the maximum loan amount.
Sample Input–
7 5
100
400
300
100
500
101
400
Sample Output–
500
Notes -
30%:n <=10
60%: n<=1000
100%: n<=100000
Code -
#include<iostream>
#include<cstdio>
using namespace std;
int n,m,l,r,s,t,fy,mid,a[100005];
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i)
{
scanf("%d",&a[i]);
l=max(l,a[i]),r+=a[i];
}
while(l<=r)//二分答案
{
mid=(l+r)/2;
s=mid-a[1];
t=m,fy=0;
for (int i=2;i<=n;++i)
if (s>=a[i]) s-=a[i];
else if(t>0) s=mid-a[i],t--;
else { fy=1; break; }
if (fy) l=mid+1;
else r=mid-1;
}
printf("%d",l);
return 0;
}