QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#93760 | #6194. Knapsack Problem | cabbit | TL | 165ms | 4084kb | C++23 | 2.3kb | 2023-04-02 09:57:24 | 2023-04-02 09:57:27 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int64_t INF = 1e18;
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int T; cin >> T;
while (T--) {
int N; cin >> N;
vector<int> C((1 << N) - 1); for (int &c : C) cin >> c;
vector<int> A(N); for (int &a : A) cin >> a;
int L = 1 << (N * N);
// cerr << "L = " << L << '\n';
vector<int64_t> dp(L, -INF);
dp[0] = 0;
auto encode = [&](vector<int> c) {
int x = 0;
for (int a : c) x = x << N | a;
return x;
};
auto decode = [&](int x) {
vector<int> c(N);
for (int i = N - 1; i >= 0; --i) {
c[i] = x & ((1 << N) - 1);
x >>= N;
}
return c;
};
for (int z = 0; z < 30; ++z) {
// cerr << "z = " << z << '\n';
for (int i = 1; i < 1 << N; ++i) {
// cerr << "i = " << i << '\n';
vector<int64_t> ndp = dp;
for (int x = 0; x < L; ++x) {
if (dp[x] < 0) continue;
auto c = decode(x);
// cerr << "from "; for (int a : c) cerr << a << ' '; cerr << '\n';
for (int j = 0; j < N; ++j) if (i >> j & 1) c[j]++;
// cerr << "to "; for (int a : c) cerr << a << ' '; cerr << '\n';
int nx = encode(c);
ndp[nx] = max(ndp[nx], dp[x] + (int64_t(C[i - 1]) << z));
}
dp.swap(ndp);
}
vector<int64_t> ndp(L, -INF);
for (int x = 0; x < L; ++x) {
if (dp[x] < 0) continue;
auto c = decode(x);
bool ok = true;
for (int i = 0; i < N; ++i) {
if ((c[i] & 1) == (A[i] >> z & 1)) {
c[i] >>= 1;
} else {
ok = false;
break;
}
}
if (ok) {
int nx = encode(c);
ndp[nx] = max(ndp[nx], dp[x]);
}
}
dp.swap(ndp);
}
cout << dp[0] << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 165ms
memory: 4084kb
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...