小木棍

数据好强啊,但是感觉我第二次写的应该是哪里出了问题?

 

27分代码(wa了好多点但是没有t):

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 int a[70],vis[70];
 8 int n,cnt = 0,tot = 0,maxl = 0;
 9 
10 bool check(int len,int nowl,int lastid){
11     int flag = 1,fail = -1;
12     for(int i = lastid+1;i <= n;i++)if(!vis[i]&&a[i] != fail){
13         flag = 0;
14         if(nowl + a[i] == len){
15             vis[i] = 1;
16             if(check(len,0,0))return true;
17             vis[i] = 0;
18             return false;
19         }
20         else if(nowl + a[i] < len){
21             vis[i] = 1;
22             if(check(len,nowl+a[i],i))return true;
23             vis[i] = 0;
24             fail = a[i];
25         }
26     }
27     if(flag)return true;
28     else return false;
29 };
30 
31 int main(){
32     cin >> n;
33     for(int i = 1;i <= n;i++){
34         cin >> a[++cnt];
35         if(a[cnt] > 50)cnt--;
36         else tot += a[cnt],maxl = max(maxl,a[cnt]);
37     }
38     n = cnt;
39     sort(a+1,a+n+1);
40     reverse(a+1,a+n+1);
41     for(int l = maxl;l <= tot;l++)
42         if(tot % l == 0&&check(l,0,0)){
43             cout << l << '\n';
44             return 0;
45         }
46 }

 

 

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

int a[70],vis[70];
int n,cnt = 0,tot = 0,maxl = 0,len;

bool check(int cnt,int nowl,int lastid){
    if(cnt > tot/len)return true;
    int fail = -1;
    for(int i = lastid+1;i <= n;i++)if(!vis[i]&&a[i] != fail){
        if(nowl + a[i] == len){
            vis[i] = 1;
            if(check(cnt+1,0,0))return true;
            vis[i] = 0;
            return false;
        }
        if(nowl + a[i] < len){
            vis[i] = 1;
            if(check(cnt,nowl+a[i],i))return true;
            vis[i] = 0;
            fail = a[i];
            if(nowl == 0||nowl+a[i] == len)return false;
        }
    }
    return false;
};

int main(){
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> a[++cnt];
        if(a[cnt] > 50)cnt--;
        else tot += a[cnt],maxl = max(maxl,a[cnt]);
    }
    n = cnt;
    sort(a+1,a+n+1);
    reverse(a+1,a+n+1);
    for(len = maxl;len <= tot;len++)
        if(tot % len == 0&&check(1,0,0)){
            cout << len << '\n';
            return 0;
        }
}

 

posted @ 2019-09-23 00:15  TIH_HIT  阅读(103)  评论(0编辑  收藏  举报