小木棍

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

 

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;
        }
}

 

#include<iostream>#include<cstdio>#include<algorithm>usingnamespacestd; 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)returntrue; 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))returntrue; vis[i] = 0; returnfalse; } if(nowl + a[i] < len){ vis[i] = 1; if(check(cnt,nowl+a[i],i))returntrue; vis[i] = 0; fail = a[i]; if(nowl == 0||nowl+a[i] == len)returnfalse; } } returnfalse; }; 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'; return0; } }

转载于:https://www.cnblogs.com/Wangsheng5/p/11570099.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值