Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

唔 这种二分的终止条件还是没想太明白 贴个代码以后再看

Posted by songruirui at 2013-03-26 16:07:37 on Problem 3273
#include <iostream>
#include <algorithm>
using namespace std;
int money[100005];
int main()
{
	int N;//总共的天数
	int M;//分成fajomonths月数
	cin>>N>>M;
	int low=0;
	int high=0;
	for(int i=1;i<=N;i++)
	{	
		cin>>money[i];
		high+=money[i];
		if(money[i]>low)
			low=money[i];
	}
	int mid;
	while(low<=high)
	{
		mid=(low+high)/2;
		int sum=0;
		int duanshu=1;
		for(int i=1;i<=N;i++)
		{
			sum+=money[i];
			
			if(sum<=mid)
			;//计算在月最大消费控制在mid下时能分的段数
			else 
			{
				sum=money[i];
				duanshu++;
			}
		}
		if(duanshu>M)//分的段数比需要的大,说明标准高了
			low=mid+1;
		else if(duanshu<M)//分的段数比需要的小,说明标准低了
			high=mid-1;
		else if(duanshu==M)//分的段数和需要的相同,这时还有可能再严格一点,即使每月的最大支出更小一点
			high=mid-1;
	}
	cout<<low<<endl;
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator