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 15:26:42 on Problem 3258
#include <iostream>
#include <algorithm>
using namespace std;
int dist[50005];
int main()
{
	int L;//表示最长的距离
	int N;//表示中间的石头数
	int M;//表示最多可移除的石头数
	cin>>L>>N>>M;
	int low,high;
	dist[N+1]=L;
	low=L;
	high=L;
	for(int i=1;i<=N;i++)
	{	
		cin>>dist[i];
	}
	
	sort(dist,dist+N+1);
	for(int i=1;i<=N;i++)
	{
		if(low>dist[i]-dist[i-1])
			low=dist[i]-dist[i-1];
	}
	while(low<=high)
	{
		int mid=(high+low)/2;
		//判断在当前的mid下移除的的石头数delnum
		int delnum=0;
		int sumlength=0;
		for(int i=1;i<=N;i++)
		{
			
			sumlength=dist[i]-dist[i-1]+sumlength;
			if(sumlength<mid)
			{
				
				delnum++;
			}
			else
				sumlength=0;
		}
		if(delnum<=M)
			low=mid+1;
		else
			high=mid-1;
	}
	cout<<high<<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