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

Re:请问有人能够将64的那组BT数据控制在1S之内么?

Posted by 00448322 at 2009-04-22 19:10:29 on Problem 1011
In Reply To:请问有人能够将64的那组BT数据控制在1S之内么? Posted by:zentropy at 2007-09-02 16:03:34
这个应该行

#include <iostream>
#include <cstdlib>

using namespace std;
int num[64];
int next[64];
int n;
bool used[64];
int len;

int searched[65][65][50*65];

int cmp(const void* a,const void* b)
{
	return *((int*)b)-*((int*)a);
}


inline int sumLen(int m)
{
	int sum=0;
	for(int i=m; i<n; i++)
	{
		if(used[i]==false)
			sum+=num[i];
		
	}
	return sum;
}

void Init(int total, int sum)
{

	for(int i=0; i<=total; i++)
	{
		for(int j=0; j<=total; j++)
		{
			for(int k=0; k<=sum; k++)
				searched[i][j][k]=0;
		}
	}
}

bool stick(int total, int start, int left)
{
	int i;
	if(total==n)
		return true;
	for(i=start; i<n; )
	{
		if(used[i]==false)
		{
			if(left>num[i])
			{
				left-=num[i];
				used[i]=true;
				if(searched[total+1][start+1][left]==1)
					return true;
				else if(searched[total+1][start+1][left]==-1)
					;
				else
				{
					if(stick(total+1,start+1,left))
					{
						searched[total+1][start+1][left]=1;
						return true;
					}
					else
						searched[total+1][start+1][left]=-1;
				}
				left+=num[i];
				used[i]=false;
				if(start==0)
					break;
			}
			else if(left==num[i])
			{
				used[i]=true;
				if(searched[total+1][0][len]==1)
					return true;
				else if(searched[total+1][0][len]==-1)
					;
				else 
				{
					if(stick(total+1,0,len))
					{
						searched[total+1][0][len]=1;
						return true;
					}
					else
						searched[total+1][0][len]=-1;
				}
				used[i]=false;
				break;
			}
			i=next[i];
		}
		else
			i++;
		if(sumLen(i)<left)
			return false;

	}
	return false;
}

int main()
{
	cin>>n;
	int i,j;
	int sum;
	while(n)
	{
		sum=0;
		for(i=0; i<n; i++)
		{
			cin>>num[i];
			sum+=num[i];
			used[i]=false;
		}
		qsort(num,n,sizeof(int),cmp);
		int start=0;
		int end=0;
		while(end<n)
		{
			while(end<n&&num[start]==num[end])
				end++;
			for(j=start; j<end; j++)
			{
				next[j]=end;
			}
			start=end;
		}
		for(i=num[0]; i<=sum; i++)
		{
			//			cout<<"i"<<i<<endl;
			if(sum%i==0)
			{
				Init(n,sum);
				len=i;
				if(stick(0,0,len))
					break;
			}
		}
		cout<<len<<endl;
		cin>>n;
		
	}
	return 0;
}


> Input
> 64
> 40 40 30 35 35
> 26 15 40 40 40
> 40 40 40 40 40
> 40 40 40 40 40
> 40 40 40 43 42
> 42 41 10 4 40
> 40 40 40 40 40
> 40 40 40 40 40
> 40 40 40 25 39
> 46 40 10 4 40
> 40 37 18 17 16
> 15 40 40 40 40
> 40 40 40 40
> Output
> 454
> 
> 虽然AC了,不过这组数据得6S左右,感觉再优化下去我会疯掉的。。这几天就一直在捣鼓这个题目,这个数据死活控制不住。。

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