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

无语啊,c++wa,g++32ms过了

Posted by vook at 2017-04-03 13:47:09 on Problem 1011
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int n;
int vis[100];
int a[100];
int m;
int cnt;
int aim;
int flag;
vector<int >d;
int cmp(int a,int b)
{
    return a>=b;
}
void dfs(int x,int l,int first)
{
    if(x==0)
    {
        cnt++;
        dfs(aim,first+1,first+1);
        if(!flag)
            cnt--;
        return;
    }
    if(cnt==m-1)
    {
        flag=1;
        return;
    }
    int i;
    for(i=l; i<n; i++)
    {
        if(a[i]==a[i-1]&&!vis[i-1])
            continue;
        if(!vis[i]&&a[i]<=x)
        {
            vis[i]=1;
            dfs(x-a[i],i+1,first);
            if(!flag)
            {
                vis[i]=0;
                if(x==aim)
                    return;
            }
            else
                return;
        }
    }
    if(i==n)
    {
        flag=0;
        return;
    }
}
int main()
{
    while(scanf("%d",&n)&&n)
    {
        d.clear();
        memset(a,0,sizeof(a));
        int ans=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            ans+=a[i];
        }
        sort(a,a+n,cmp);
        for(int i=a[n-1]; i<=ans; i++)
        {
            if(ans%i==0)
                d.push_back(i);
        }
        int i;
        for(i=0; i<d.size(); i++)
        {
            memset(vis,0,sizeof(vis));
            flag=0;
            cnt=0;
            aim=d[i];
            m=ans/d[i];
            dfs(aim,0,0);
            if(flag)
            {
                break;
            }
        }
        printf("%d\n",aim);
    }
    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