45分求调 QAQ
  • 板块P1120 小木棍
  • 楼主jhafyghk
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/6/25 16:17
  • 上次更新2024/6/25 20:01:20
查看原帖
45分求调 QAQ
1079589
jhafyghk楼主2024/6/25 16:17
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
bool st[90];
int n;
int a[90];
int cnt;
bool cmp(int a, int b) {
    return a > b;
}
bool dfs(int x, int num, int deep) {
    if (num == cnt) {
        return true;
    }
    if (deep == cnt + 1) {
        if (num == cnt) {
            return true;
        }
        return false;
    }
    if (st[deep]) {
        return dfs(x, num, deep + 1);
    }
    int len = 0;
    bool f = false;
    int t = num;
    for (int i = deep; i <= cnt; i++) {
        if (!st[i] && len + a[i] <= x) {
            st[i] = true;
            t++;
            len += a[i];
            if (len == x) {
                f = true;
                if (dfs(x, t, deep + 1)) 
                    return true;
                else
                    return false;
                
            }
        }
    }
    return false;
}
int main() {
    cin >> n;
    int maxx = 0;
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        if (x > 50)
            continue;
        a[++cnt] = x;
        maxx = max(maxx, a[i]);
        sum += a[i];
    }
    sort(a + 1, a + 1 + cnt, cmp);
    for (int i = 1; i <= sum; i++) {
        memset(st, false, sizeof(st));
        if (dfs(i, 0, 1)) {
            cout << i << endl;
            break;
        }
    }
    return 0;
}
2024/6/25 16:17
加载中...