poj3273二分法
/*二分法:对dollar二分从mx和sum处开始二分*/
#include <iostream>
#define Max 100000
using namespace std;
int n,m;
int dollar[Max];
bool judge(int x)//计算当mid为x时可以分成多少个"fajomonths"并与m比较。
{
int num=1;
int sum=0;
for (int i=0;i<n;i++)
{
sum+=dollar[i];
if(sum>x)
{
sum=dollar[i];num++;
}
}
if(num<=m)
return true;//right=mid-1;
else
return false;//left=mid+1;
}
int main()
{
while(cin>>n>>m)
{
int sum=0,mx=0;
for (int i=0;i<n;i++)
{
scanf("%d",&dollar[i]);
if(mx<dollar[i])
mx=dollar[i];
sum+=dollar[i];
}
int left=mx,right=sum;
while(left<=right)
{
int mid=(left+right)>>1;
if(judge(mid))
{
right=mid-1;
}
else
{
left=mid+1;
}
}
cout<<left<<endl;
}
return 0;
}