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

第一次来poj 做这个题目 试了一下讨论区里面的数据 都能过 也不会超时 但提交就是runtime error 有人知道为什么吗?

Posted by titanxxh at 2010-05-17 19:34:30 on Problem 1011
代码:
写得比较丑陋 大家别笑我
//sticks

//#include "stdafx.h"
#include <iostream>
//#include <fstream>

using namespace std;

const int maxlen=50;
const int maxn=64;

struct node
{
	int qtt,last,now;
};

void sort(int* a,int n)
{
	for (int i=0;i<n-1;i++)
		for (int j=i+1;j<n;j++)
			if (a[i]<a[j]) 
			{
				int temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
}

bool check(int* num,int left,int des,int* make)
{
	int n=left;
	int a[maxn];
	for (int i=0;i<n;i++)
	{
		a[i]=num[i];
	}
	node pkg[maxlen*maxn+1];
	for (int i=1;i<=des;i++)
		pkg[i].qtt=-1;
	pkg[0].qtt=0;
	for (int i=0;i<n;i++)
	{
		for (int j=des-a[i];j>=0;j--)
		{
			if (pkg[j].qtt!=-1)
			{
				if (pkg[j+a[i]].qtt==-1)
				{
					pkg[j+a[i]].qtt=pkg[j].qtt+1;
					pkg[j+a[i]].last=j;
					pkg[j+a[i]].now=a[i];
					continue;
				}
				if (pkg[j+a[i]].qtt>pkg[j].qtt+1) 
				{
					pkg[j+a[i]].qtt=pkg[j].qtt+1;
					pkg[j+a[i]].last=j;
					pkg[j+a[i]].now=a[i];
				}
			}
		}
	}
	int amt=0;
	if (pkg[des].qtt!=-1)
	{
		int pos=des;
		while (pos!=0)
		{
			make[amt]=pkg[pos].now;
			amt++;
			pos=pkg[pos].last;
		}
	}
	else return false;
	return true;
}

void dfs(int* num,int left,int des,int* list,int& t,int*temp,bool* used,int l,int now,int start)
{
	int pre=-1;
	for (int i=start;i<left;i++)
	{
		if ((used[i]) || (pre==num[i]) || (now+num[i]>des)) continue;
		now+=num[i];
		pre=num[i];
		used[i]=true;
		l++;
		temp[l]=num[i];
		if (now==des)
		{
			list[t]=l;
			for (int j=1;j<=l;j++)
				list[t+j]=temp[j];
			t+=l+1;
		}
		else dfs(num,left,des,list,t,temp,used,l,now,i);
		now-=num[i];
		used[i]=false;
		l--;
	}
}

bool calc(int* num,int left,int des)
{
	int n=left;
	if (n==0) return true;
	else
	{
		int make[maxn];
		bool chk=check(num,left,des,make);
		if (!chk) return false;
		else
		{
			int a[maxn];
			bool used[maxn];
			int temp[maxn];
			int t=0;
			for (int i=0;i<n;i++)
			{
				a[i]=num[i];
				used[i]=false;
			}
			int list[maxn*maxlen];
			for (int i=0;i<maxn*maxlen;i++)
				list[i]=-1;
			dfs(a,left,des,list,t,temp,used,0,0,0);
			int pos=0;
			while (list[pos]!=-1)
			{
				int a[maxn];
				for (int i=0;i<n;i++)
				{
					a[i]=num[i];
				}
				for (int i=1;i<=list[pos];i++)
				{
					for (int j=0;j<left;j++)			
						if (list[pos+i]==a[j]) 
						{
							a[j]=0;
							break;
						}				
				}
				int new_left=0;
				int b[maxn];
				for (int i=0;i<n;i++)
					if (a[i]!=0)
					{
						b[new_left]=a[i];
						new_left++;
					}
				return calc(b,new_left,des);
				pos+=list[pos]+1;
			}
		}
	}
}

int main()
{
	//ifstream inf("input.txt");
	int answer[maxn];
	int tt=0;
	do
	{
		int n;
		cin>>n;
		if (n==0) break;
		int a[maxn];
		int total=0;
		for (int i=0;i<n;i++)
		{
			cin>>a[i];
			total+=a[i];
		}
		sort(a,n);
		bool find=false;
		for (int ys=a[0];ys<=total/2;ys++)
		{
			if (total%ys==0)
			{
				if (calc(a,n,ys))
				{
					answer[tt]=ys;
					tt++;
					find=true;
					break;
				}
			}
		}
		if (!find) 
		{
			answer[tt]=total;
			tt++;
		}
	}
	while (true);
	for (int i=0;i<tt;i++)
		cout<<answer[i]<<endl;
	//inf.close();
	//system("pause");
	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