Problem B
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 19 Accepted Submission(s) : 5
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 (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.
FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 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.
#include<iostream>
#include<algorithm>
using namespace std;
int n, m, a[100010], low, high, mid;
int judge(int mid1)
{
int sum = 0, ans = 1;//开始为整体为一组
for (int i = 1; i <= n; i++)
{
if (sum + a[i] <= mid1)
sum += a[i];
else
{
sum = a[i];
ans++;
}
}
if (ans > m)
return 1;//组数偏多mid偏小low=mid+1
else
return 0;//组数偏少mid偏大
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
low = max(a[i], low);
high += a[i];
}
while (low <= high)
{
mid = (low + high) / 2;
if (judge(mid))//
low = mid + 1;
else
high = mid - 1;
}
cout << mid << endl;
return 0;
}