QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#59715#1362. Bad PackingSa3tElSefr#RE 2ms3604kbC++202.8kb2022-10-31 21:46:042022-10-31 21:46:05

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-31 21:46:05]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:3604kb
  • [2022-10-31 21:46:04]
  • 提交

answer

#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>

#define ll long long
#define ld  double
using namespace std;

const int N = 1e5 + 5, M = 1005, lg = 19, mod = 998244353;

const int tM = 20;

bool dp[tM][tM], mark[tM][tM];

int n, cap, a[M];


bool solve(int idx, int rem) {
    // cout << idx << ' ' << rem << '\n';
    if(idx == n) return (rem == 0);
    if(mark[idx][rem]) return dp[idx][rem];
    mark[idx][rem] = 1;

    bool op1 = solve(idx + 1, rem);
    bool op2 = 0;
    if(a[idx] <= rem) op2 = solve(idx + 1, rem - a[idx]);

    return dp[idx][rem] = (op1 | op2);
}

ll getOptimal() {
    ll ans = accumulate(a, a + n, 0);
    for(int mask = 0; mask + 1 < (1 << n); mask++) {
        ll mn = 10000, tAns = 0;
        for(int bit = 0; bit < n; bit++) {
            if(mask >> bit & 1) {
                tAns += a[bit];
            }
            else mn = min(mn, 0LL + a[bit]);
        }
        if(tAns <= cap && tAns + mn > cap) {
            ans = min(ans, tAns);
        }
    }
    return ans;
}


int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    srand(time(0));


    // cin >> n >> cap;
    int tc = 0;
    while(1) {
        // cout << ++tc << endl;
        // memset(mark, 0, sizeof mark);
        // n = rand() % 3 + 4;
        // cap = rand() % 5 + 1;
        cin >> n >> cap;
        for(int i = 0; i < n; i++) {
            cin >> a[i];
            // a[i] = a[i] % cap + 1;
        }

        // ll optimalAns = getOptimal();
        sort(a, a + n);
        ll total = accumulate(a, a + n, 0LL);

        if(total <= cap) {
            cout << total;
            return 0;
        }

        ll mn = total, pref = 0;
        for(int i = cap; i >= 0; i--) {
            if(solve(0, i)) {
                mn = i;
                break;
            }
        }
        // cout << "HERE " << mn << '\n';
        int mxJ = -1;
        for(int i = 0; i < n; i++) {
            if(pref <= cap) {
                for(int j = 0; j < a[i]; j++) {
                    // cout << "j " << i << ' ' << j << ' ' << a[i] << '\n';
                    mxJ = max(mxJ, j);
                    if(cap - pref - j < 0) {
                        // cout << "BREAK " << i << ' ' << j << ' ' << a[i] << '\n';
                        break;
                    }
                    if(solve(i + 1, cap - pref - j)) {
                        // cout << "cap - j" << ' ' << cap - j << ' ' << cap << ' ' << j << '\n';
                        mn = min(mn, 0LL + cap - j);
                    }
                }
            }
            pref += a[i];
        }
        cout << mn;
        return 0;
    }






    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 3604kb

input:

4 10
9
6
5
7

output:

5

result:

ok single line: '5'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3580kb

input:

10 25
1
1
1
2
2
3
3
4
2
1

output:

20

result:

ok single line: '20'

Test #3:

score: -100
Runtime Error

input:

77 9383
203
6771
1608
6509
3213
3597
3416
3011
2241
740
5564
3113
360
3229
5819
5589
5210
4519
5270
6067
10
9147
4171
920
8325
263
8097
3400
9214
3927
8804
4805
8388
1211
523
3799
1124
8573
7491
5527
8026
8529
2510
6430
6171
1405
4820
7662
2449
7264
1419
6320
7272
3327
7042
1517
8326
881
2199
4664
9...

output:


result: