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
北京大学《ACM/ICPC大学生程序设计竞赛训练》暑期课面向全球招生!

如此暴力的代码也WA??

Posted by dynamic_study at 2009-08-31 16:40:18 on Problem 2549 and last updated at 2009-08-31 16:41:37
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(long a,long b)
{return a>b;}
long a[1001];
int n;
int bins(long temp)
{
	int left=0,right=n-1,mid;
	while(left<=right)
	{
		mid=(left+right)>>1;
		if(a[mid]<temp)
			right=mid-1;
		else if(a[mid]>temp)
			left=mid+1;
		else
			return mid;
	}
	return -1;

}
int main()
{
	
	int i,j,k;
	long mid,ans;
	bool loop;
	while(scanf("%d",&n)!=EOF&&n){
	for(i=0;i<n;i++)
		scanf("%ld",&a[i]);
	sort(a,a+n,cmp);
	loop=false;
	if(n<4)
		goto next;
	for(i=0;i<n-2;i++)
	{
		for(j=i+1;j<n-1;j++)
		{
			for(k=j+1;k<n;k++)
			{
				mid=a[i]+a[j]+a[k];
				ans=bins(mid);
				if(ans!=-1&&ans!=i&&ans!=j&&ans!=k)
				{
					loop=true;
					goto next;
				}
			}

		}
	}
next:
	if(loop)
            printf("%ld\n",mid);
	else
	   printf("no solution\n");
	}
	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