题目链接:点击打开链接
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 (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.
Input
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
题目大意:给出n个数 代表N个月的价值,m代表可以分成m份(连续的),求在任意一个连续区间内的最小,其中最大是多少
基本思路,二分求解,下界是把它分成n份最大的,上界是n个值的和,每次二分一下,判断是否合适,分多了还是分少了,然后在继续超找
<span style="font-size:18px;">#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
int a[100100];
using namespace std;
int main()
{
int n,m;
int mid,low,high;
while( cin>>n>>m)
{
high=0;
low=-1;
for(int i=1; i<=n; i++)
{
cin>>a[i];
high+=a[i];///上界
if(low<a[i])low=a[i];///下界
}
mid=(low+high)/2;///二分
while(low<high)
{
int t=1;
int sum=0;
for(int i=1; i<=n; i++)
{
/*if(sum+a[i]<=mid)
sum=sum+a[i];
else
{
sum=a[i];
t++;
}*/
sum=sum+a[i];
if(sum>mid)///和大于最大值后,将a[i]当作下一个连续区间的第一个值
{
t++;
i--;
sum=0;
}
}
if(t>m)///分多了
{
low=mid+1;
}
else high=mid-1;///分少了
mid=(low+high)/2;
//cout<<t<<endl;
}
cout<<mid<<endl;
}
return 0;
}
</span>