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

老是WA,可是自己想的数据都过掉了 谁来帮着看一下啊,感激不尽

Posted by dicken at 2007-10-05 11:43:21 on Problem 2549
先是计算a+b 然后枚举d,c 在a+b中查找d-c
#include<stdio.h>
#include <algorithm>
using namespace std;
struct result
{
	long s;
	long x;
};
	
	result sum[1000000];
	long set[10000];
	
bool cmp(const result&p, const result&q)
{
	if (p.s==q.s) return p.x<q.x;  
	return p.s<q.s;
}

bool tofind(long key,long x,long sumn)
{
	long M,L=0,R=sumn-1,ML,MR;
	while (L<=R)
	{
		M=(L+R)/2;
		if (sum[M].s==key)
		{
			if ((sum[M].x!=x) && ((sum[M].s-sum[M].x)!=x))
				return true;
			else
			{
				ML=M-1;
				MR=M+1;
				while(ML>=0)
				{
					 if (sum[ML].s!=key) break;
					 if ((sum[ML].x!=x) && ((sum[ML].s-sum[ML].x)!=x)) return true;
					 ML--;
				}
				while(MR<sumn)
				{
					 if (sum[MR].s!=key) break;
					 if ((sum[MR].x!=x) && ((sum[MR].s-sum[MR].x)!=x)) return true;
					 MR++;
				}
				return false;		
			}																	
			 
		};

		if (sum[M].s>key) R=M-1;
		else L=M+1;
	}
	return false;	
}

int main()
{
	long n,c,sumn;
	bool f=false;
	while (scanf("%ld",&n)==1 && n)
	{
		for (long i=0;i!=n;i++)
			scanf("%ld",&set[i]);	//input
		sort(set,set+n);
		sumn=n*(n-1)/2;
		c=0;
		for (long i=0;i!=n-1;i++)
			for (long j=i+1;j!=n;j++)
				{
					sum[c].s=set[i]+set[j];	//plus
					sum[c].x=set[i];
					c++;
				};
		
		sort(sum,sum+sumn,cmp); //sort
	    
	    f=false;
	    for (long i=n-1;i>=0;i--)
	    	{
	    	 for (long j=0;j!=n;j++)
				{
					if (i==j) continue;
					if (tofind(set[i]-set[j],set[j],sumn)) { f=true;printf("%ld\n",set[i]);break;}
				}
			 if (f) break;
	    	}
		if (!f) 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