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 dovegx at 2012-03-30 16:56:20 on Problem 3258
#include<iostream>
using namespace std;
#define N 50005
#define M 2000000000
int rock[N];

void quick(int i,int j)
{
	if(i>=j)
		return;
	int a=rock[i];
	int x=i,y=j;
	while(x<y)
	{
		while(x<y&&rock[y]>=a)y--;
		rock[x]=rock[y];
		while(x<y&&rock[x]<=a)x++;
		rock[y]=rock[x];
	}
	rock[x]=a;

	quick(i,x-1);
	quick(x+1,j);

}
int main()
{
	int l,n,m;
	int sum,min,mid,s,cnt,d;
	while((scanf("%d %d %d",&l,&n,&m))!=EOF)
	{
		sum=0;
		min=M;
		for(int i=0;i<n;i++)
		{
			scanf("%d",rock+i);	
		}
		rock[n]=l;
		quick(0,n);
		sum=l;
		for(int i=1;i<=n;i++)
		{
			d=rock[i]-rock[i-1];
			if(min>d)
				min=d;
		}
		s=0;
		mid=(sum-min+1)/2+min;
		while(min<sum)
		{
			cnt=0;
			s=0;
			for(int i=0;i<=n;i++)
			{
				d=rock[i]-s;
				if(d>=mid)
				{
					cnt++;
					s=rock[i];
				}
			}
				
				if(cnt>=n-m+1)
					min=mid;
				else
					sum=mid-1;
				mid=(sum-min+1)/2+min;
			
		}

		cout<<sum<<endl;/*这是AC的代码,可是为什么把这一句改成cout<<min<<endl;就变成了WA,
						min和sum不应该是一样的吗*/
	}
}

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