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

终于AC了!!!!!!!!!!!!!!!!!!DPS!!!!!!!!!!!!!!

Posted by 2014111896 at 2015-07-27 21:14:02 on Problem 1011
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
int s[65],sum=0,Maylen,num,n;
bool used[65];
int cmp(const void* a,const void* b)
{
    return *(int*)b-*(int*)a;
}
bool dfs(int sticks,int curlen,int pos)
{
    int i;
    if(sticks==num)
        return true;
    for(i=pos+1;i<n;i++)
    {
        if(used[i])
            continue;
        if(curlen+s[i]==Maylen)
        {
            used[i]=true;
            if(dfs(sticks+1,0,-1))
                return true;
            used[i]=false;
            return false;
        }
        else if(curlen+s[i]<Maylen)
        {
            used[i]=true;
            if(dfs(sticks,curlen+s[i],i))
                return true;
            used[i]=false;
            if(!curlen)
                return false;
            while(s[i]==s[i+1])
                i++;
        }
    }
    return false;
}
using namespace std;
int main()
{
    while(scanf("%d",&n)==1)
    {
        if(!n)
            break;
        sum=0;
        for(int i=0;i<n;i++)
        {
            cin>>s[i];
            sum+=s[i];
        }
        qsort(s,n,sizeof(int),cmp);
        for(Maylen=s[0];Maylen<=sum;Maylen++)
        {
            if(sum%Maylen)
                continue;
            else
                num=sum/Maylen;
            memset(used,false,sizeof(used));
            if(dfs(1,0,-1))
            {
                cout<<Maylen<<endl;
                break;
            }
        }
    }
    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