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

WHY WA

Posted by 350053879 at 2008-03-14 08:55:11 on Problem 3400
#include<iostream>
#include<string>
//#include<algorithm>
using namespace std;

typedef struct stone
{
	int p;
	int v;
}stone;

stone *s;
int n,d;      //
bool used[12];//标记数组。
int Imax=0;    //当前的最大值。

void search(int k,int curA,int curB,int valueA,int valueB,int flag)
{//flag表示当前放置哪个。0  A, 1 B.
	if(k==n)
	{
		if(valueB>Imax) Imax=valueB;
		return;
	}
	else
	{
		if(curA==0&&curB==0)
		{//初始化为刚开始必须是A.
			//for(int i=0;i<n;i++)
			//{
			//	if(!used[i])
			//	{
					used[0]=true;
					search(k+1,curA+s[0].p,curB,valueA+s[0].v,valueB,0);
					used[0]=false;
			//	}
			//}
		}

		else
		{			
			for(int i=0;i<n;i++)
			{
				if(!used[i]&&!flag)
				{			
					if( curA+s[i].p-curB>=d)
					{//大 flag ->1.
						used[i]=true;
						search(k+1,curA,curB+s[i].p,valueA,valueB+s[i].v,1);
						used[i]=false;
					}
					else
					{
						used[i]=true;
						search(k+1,curA+s[i].p,curB,valueA+s[i].v,valueB,0);
						used[i]=false;
					}
				}
				if(!used[i]&&flag)
				{
					if(curB+s[i].p-curA>=d)
					{
						used[i]=true;
						search(k+1,curA+s[i].p,curB,valueA+s[i].v,valueB,0);
						used[i]=false;
					}
					else
					{
						used[i]=true;
						search(k+1,curA,curB+s[i].p,valueA,valueB+s[i].v,1);
						used[i]=false;
					}
				}
			}
		}
	}
}

int main()
{
	while(scanf("%d%d",&n,&d)!=EOF)
	{
		s=new stone[n+1];

		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&s[i].p,&s[i].v);
		}
		Imax=0;
		//cur=0;
		memset(used,false,sizeof(used));
		search(0,0,0,0,0,0);
		printf("%d\n",Imax);
		delete []s;
	}
	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