POJ 3273

二分,至今仍很纠结

#include <stdio.h>
#define maxn 100010
int n,m;
int judge(int mid,int* ex)
{
	int gr=1;//!!!!!!!!!!!!
	int sum=0;
	int i;
	for(i=0;i<n;i++)
	{
		if((sum+ex[i])<=mid)
		{
			sum+=ex[i];
		}
		else
		{
			gr++;
			sum=ex[i];//!!!!!!!!!!
		}
	}
	if(gr>m) return 1;//说明小了
    else return 0;
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		scanf("%d",&m);
		int ex[maxn];
		int i;
		int high=0,low=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&ex[i]);
			if(low<ex[i]) low=ex[i];//下界能分成n组
			high+=ex[i];
		}
		high++;//上界能分成1组
		int mid;
		while(low<high)
		{
			mid=low+(high-low)/2;
			if(judge(mid,ex))
				low=mid+1;
			else
				high=mid;
		}
		printf("%d\n",high);//输出high和low都可以
	}
	return 0;
}


posted @ 2012-06-13 18:30  LJ_COME!!!!!  阅读(115)  评论(0编辑  收藏  举报