Description
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money ( ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) days. FJ wants to create a budget for a sequential set of exactly M ( ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of or more consecutive days. Every day is contained in exactly one fajomonth. FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.
Input
Line : Two space-separated integers: N and M
Lines ..N+: Line i+ contains the number of dollars Farmer John spends on the ith day
Output
Line : The smallest possible monthly limit Farmer John can afford to live with.
Sample Input
Sample Output
Hint
If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $ in any month. Any other method of scheduling gives a larger minimum monthly limit.
Source
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
#define N 100006
#define inf 1<<30
int n,m;
int a[N];
bool solve(int x){
int sum=;
int group=;
for(int i=;i<n;i++){
if(sum+a[i]<=x){
sum+=a[i];
}
else{
sum=a[i];
group++;
}
}
if(group>=m) return false;//如果当前的mid值能分成大于等于m的组数,说明mid太小,mid变大的时候组数才能变少
return true;
}
int main()
{ while(scanf("%d%d",&n,&m)==){
int high=;
int low=;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
high+=a[i];
if(a[i]>low){
low=a[i];//把花费最多的那天当做是最低low(相当于把n天分为n组)
}
} while(low<high){ int mid=(low+high)>>;
if(solve(mid)){
high=mid;
}
else{
low=mid+;
}
}
printf("%d\n",low);
}
return ;
}