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

一次AC贴上代码^~^

Posted by qijinbiao at 2011-09-07 10:32:55 on Problem 3258
/*对rock间的距离进行二分*/
#include<iostream>
#include <algorithm>
#define Max 50005
using namespace std;
int rock[Max];
int l,n,m,mn;
bool judge(int x)//判断可移除的rock是否大于m
{
	int del=0;
	for (int i=1,j=0;i<=n+1;)
	{
		if(rock[i]-rock[j]<=x)
		{
			del++;i++;
		}
		else
		{
			j=i;i++;
		}
	}
	if(del>m)
		return true;//right=mid-1;
	else
		return false;//left=mid+1;	
}
int main()
{	
	while(cin>>l>>n>>m)
	{
		rock[0]=0;rock[n+1]=l;
		for (int i=1;i<=n;i++)
		{
			scanf("%d",&rock[i]);
			if(mn>rock[i])
				mn=rock[i];
		}
		sort(rock+1,rock+n+1);
		int right=l,left=mn;		
		while(left<=right)
		{
			int mid=(right+left)>>1;
			if (judge(mid))
			{
				right=mid-1;
			} 
			else
			{
				left=mid+1;
			}
		}
		cout<<left<<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