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 Lucifer at 2005-05-04 18:09:13 on Problem 1011
#include <iostream.h>
#include <memory.h>
#include <algorithm>
using namespace std;
int i,used[200],list[200],t,now,k,ed,n,total,co;
int thsort()
{
        int a,b,c;
        for (a=1;a<=n;a++)
        for (b=1;b<=n-1;b++)
        if (list[b]<list[b+1])
        {
                c=list[b];
                list[b]=list[b+1];
                list[b+1]=c;
        }
        return 0;
}
bool solve(int k, int i, int now, int or)   //k:木棍应该长度 i:当前的号数。 now:正在组装的木棍长度. or:正在组装第or根木棍。
{
        int t1,t2;
        if ((now==k)&&(or==ed)) return true;
        if (now==k)
        {
                t2=solve(k,0,0,or+1);
                return t2;
        }
	if (now==0)
	{
		for (t1=1;t1<=n&&used[t1]==1;t1++);
		used[t1]=1;
		t2=solve(k,t1,list[t1],or);
		return t2;
	}
	for (t1=i+1;t1<=n;t1++)
	if ((used[t1]==0)&&(list[t1]+now<=k))
	{
                if (list[t1]==k-now)
                {
                        used[t1]=1;
                        t2=solve(k,t1,k,or);
                        return t2;
                }//这个if 是参考的解题报告的第三条优化:如果当前的长度刚好可以把木棍未填的填上,则填上。但是加上这个剪枝条件,则Wa,不加又memory limit exccedded. 
		used[t1]=1;
		t2=solve(k,t1,now+list[t1],or);
		if (t2) return true;
                used[t1]=0;
	}
	return false;
}
bool de(int a,int b) {return bool(a-b);}
int main()
{
        while (cin>>n&&n>0)
        {
        total=0;
	for (i=1;i<=n;i++) {cin>>list[i]; total+=list[i]; }
        thsort();
//        sort(list+1,list+n+1, de);
	k=list[1]; t=false;
	do
	{
                co=0;
		memset(used,0,sizeof(used));
		ed=total/k;
                if (total%k==0) t=solve(k,0,0,1);
		k++;
	} while(!t);
	k--;
        if (t) cout<<k<<endl; else cout<<total<<endl;
        }
}

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