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

0Ms 喷血之作

Posted by zhongshijun at 2013-04-23 15:00:18 on Problem 1011
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

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

const int MAXN=65;
int n,stick[MAXN];
bool vis[MAXN];
bool dfs(int len,int InitLen,int start,int num)
{
	if(num==n)
		return true;
	int tmp=-1;
	for(int i=start;i<n;i++)
	{
		if(vis[i]||stick[i]==tmp)
			continue;
		vis[i]=true;
		if(len+stick[i]<InitLen)
		{
			if(dfs(len+stick[i],InitLen,i+1,num+1))
				return true;
			else
				tmp=stick[i];
		}
		else if(len+stick[i]==InitLen)
		{
			if(dfs(0,InitLen,0,num+1))
				return true;
			else
                tmp=stick[i];
		}
		vis[i]=false;
		if(len==0)
			break;
	}
	return false;
}

int main()
{
	while(scanf("%d",&n),n)
	{
		int SumLen=0;
		bool flag=true;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&stick[i]);
			SumLen+=stick[i];
		}
        qsort(stick,n,sizeof(stick[0]),cmp);
		for(int InitLen=stick[0];InitLen<=SumLen-InitLen;InitLen++)
		{
		    memset(vis,false,sizeof(vis));
			if(!(SumLen%InitLen)&&dfs(0,InitLen,0,0))
			{
				printf("%d\n",InitLen);
				flag=false;
				break;
			}
		}
		if(flag)
            printf("%d\n",SumLen);
	}
	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