QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#114887#6194. Knapsack Problemstd_absTL 29ms8804kbC++202.6kb2023-06-23 21:30:162023-06-23 21:30:19

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-23 21:30:19]
  • 评测
  • 测评结果:TL
  • 用时:29ms
  • 内存:8804kb
  • [2023-06-23 21:30:16]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define pii pair<int, int>
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
const int mod = 1e9 + 7, N = 53, M = 200000;

int c[16], a[4];
ll dp[2][1 << 16];
int nxt[1 << 16][16], down[1 << 16];

void build() {
    for (int i = 0; i < 1 << 16; ++i) {
        for (int j = 0; j < 15; ++j) {
            for (int k = 0; k < 4; ++k) {
                int rm = i >> (k * 4) & 15;
                if ((j + 1) >> k & 1) {
                    rm++;
                }
                if (rm == 16) {
                    nxt[i][j] = -1;
                    break;
                }
                nxt[i][j] |= rm << (k * 4);
            }
        }
        for (int k = 0; k < 4; ++k) {
            int rm = i >> (k * 4) & 15;
            rm >>= 1;
            down[i] |= rm << (k * 4);
        }
    }
}

void upd(ll &i, ll j) {
    i = max(i, j);
}

void solve() {
    int k;
    cin >> k;
    int n = (1 << k) - 1;
    for (int i = 0; i < n; ++i) {
        cin >> c[i];
    }
    for (int i = 0; i < k; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < 2; ++i) for (int j = 0; j < 1 << k * 4; ++j) {
        dp[i][j] = -1ll << 60;
    }
    dp[0][0] = 0;
    int now = 1;
    for (int bit = 0; bit < 30; ++bit) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < 1 << k * 4; ++j) {
                dp[now][j] = -1ll << 60;
            }
            for (int j = 0; j < 1 << k * 4; ++j) if (dp[now ^ 1][j] >= 0) {
                // place 0
                upd(dp[now][j], dp[now ^ 1][j]);
                // place 1
                assert(nxt[j][i] != -1);
                upd(dp[now][nxt[j][i]], dp[now ^ 1][j] + ((ll)c[i] << bit));
            }
            now ^= 1;
        }
        for (int j = 0; j < 1 << k * 4; ++j) {
            dp[now][j] = -1ll << 60;
        }
        for (int j = 0; j < 1 << k * 4; ++j) if (dp[now ^ 1][j] >= 0) {
            int cur = 0;
            for (int i = 0; i < k; ++i) if (j >> (i * 4) & 1) {
                cur ^= 1 << i;
            }
            for (int i = 0; i < k; ++i) if (a[i] >> bit & 1) {
                cur ^= 1 << i;
            }
            if (cur) {
                continue;
            }
            upd(dp[now][down[j]], dp[now ^ 1][j]);
        }
        now ^= 1;
    }
    cout << dp[now ^ 1][0] << '\n';
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    build();
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 29ms
memory: 8804kb

input:

3
2
1 2 4
4 5
3
3226252 19270256 2430652 1187613 12496062 10286165 17494834
24 85 34
4
901133 6693218 13245489 14740234 16186210 11382783 19277321 3855635 16523184 10168517 16195031 971041 10303441 8395899 11618555
529321239 218214127 92942310 207467810

output:

18
1949753378
7832404777567179

result:

ok 3 number(s): "18 1949753378 7832404777567179"

Test #2:

score: -100
Time Limit Exceeded

input:

100
4
4488409 9842893 14331290 11289097 15777479 21131954 25620334 6146092 10634508 15988956 20477387 17435190 21923584 27278027 31766493
200477197 258791439 590664017 982949628
4
5484152 19851747 25335961 6555937 12039831 26407861 31891996 10897184 16381166 30749333 36233145 17453055 22937114 37304...

output:


result: