QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#556414 | #8005. Crossing the Border | zqs | WA | 2653ms | 55156kb | C++14 | 2.6kb | 2024-09-10 17:55:30 | 2024-09-10 17:55:31 |
Judging History
answer
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
const int mod = 998244353;
inline void add(int &x, const int y) {if ((x += y) >= mod) x -= mod;}
struct node {int x, y; inline bool operator < (const node &rhs) const {return y < rhs.y;}} a[22];
int pc[1 << 22 | 1], sum[1 << 22 | 1], high[1 << 11 | 1], f[1 << 22 | 1], g[1 << 22 | 1], n, w, mid;
std::vector<int> a1[1 << 11 | 1], a2[1 << 11 | 1];
int main() {
scanf("%d%d", &n, &w);
for (int i = 0; i < n; ++ i) scanf("%d%d", &a[i].x, &a[i].y);
if (n == 1) return printf("%d %d", a[1].y, 1), 0;
std::sort(a, a + n), mid = n >> 1;
for (int i = 0; i < n; ++ i) sum[1 << i] = a[i].x;
for (int i = 1; i < 1 << n; ++ i) sum[i] = sum[i & i - 1] + sum[i & -i];
int mx = std::max(mid, n - mid);
high[0] = -1;
for (int i = 0; i < 1 << mx; ++ i) for (int j = mx - 1; j >= 0; -- j) if (i & 1 << j) {high[i] = j; break;}
for (int s = 0; s < 1 << mid; ++ s) {
for (int s0 = s; s0; s0 = s0 - 1 & s) a1[s].push_back(s0);
a1[s].push_back(0);
std::sort(a1[s].begin(), a1[s].end(), [&](const int x, const int y) {return sum[x] > sum[y];});
}
for (int s = 1; s < 1 << n - mid; ++ s) {
for (int s0 = s; s0; s0 = s0 - 1 & s) if (high[s0] < high[s]) a2[s0].push_back(s);
a2[0].push_back(s);
}
for (int s = 0; s < 1 << n - mid; ++ s)
std::sort(a2[s].begin(), a2[s].end(), [&](const int x, const int y) {return sum[x << mid] < sum[y << mid];});
memset(f, 0x7f, sizeof f);
f[0] = 0, g[0] = 1;
for (int S = 1; S < 1 << mid; ++ S)
for (int S0 = S & S - 1; ; S0 = S0 - 1 & S) if (high[S] > high[S0]) {
int cost = a[high[S ^ S0]].y;
if (f[S0] < f[S] - cost) f[S] = f[S0] + cost, g[S] = g[S0];
else if (f[S0] == f[S] - cost) add(g[S], g[S0]);
if (!S0) break;
}
for (int S = 0; S < 1 << n; ++ S) {
int l = S & (1 << mid) - 1, r = S - l >> mid, nf = 2000000000, ng = 0;
int j = (int)a2[r].size() - 1;
for (int l0 : a1[l]) {
while (j >= 0 && sum[(a2[r][j] ^ r) << mid] + sum[l ^ l0] > w) {
int r0 = a2[r][j], t = l | r0 << mid, cost = a[high[r0 ^ r] + mid].y;
if (nf < f[t] - cost) f[t] = nf + cost, g[t] = ng;
else if (nf == f[t] - cost) add(g[t], ng);
-- j;
}
int t = l0 | r << mid;
if (f[t] < nf) nf = f[t], ng = g[t];
else if (f[t] == nf) nf = f[t], add(ng, g[t]);
}
while (j >= 0) {
int r0 = a2[r][j], t = l | r0 << mid, cost = a[high[r0 ^ r] + mid].y;
if (nf < f[t] - cost) f[t] = nf + cost, g[t] = ng;
else if (nf == f[t] - cost) add(g[t], ng);
-- j;
}
}
printf("%d %d", f[(1 << n) - 1], g[(1 << n) - 1]);
return 0;
}
/*
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 22288kb
input:
5 5 3 5 1 4 2 3 2 2 2 1
output:
9 4
result:
ok 2 number(s): "9 4"
Test #2:
score: 0
Accepted
time: 63ms
memory: 26620kb
input:
18 10000000 956231 904623 1692946 1796774 1081323 1170319 3218792 2542661 3183376 3037270 1869132 1442561 35436 35018 1564635 1939950 1847344 2006043 755870 899310 1671882 2057413 1369264 1338951 3132483 3504034 2056224 1825640 1840949 1562071 1514040 1405352 2300821 2421801 2466540 3004920
output:
9391997 70
result:
ok 2 number(s): "9391997 70"
Test #3:
score: 0
Accepted
time: 380ms
memory: 32176kb
input:
20 10000000 1289384 1416015 1692778 1966748 1747794 1708650 2885387 2925290 2516650 2410838 2202363 2092667 368691 407497 1897764 1902790 180541 224758 1089173 1075924 2005212 1743637 702568 566295 465783 369143 2722863 2902398 174068 150211 513930 519657 1634023 1313239 1133070 1040937 961394 11066...
output:
6331196 89
result:
ok 2 number(s): "6331196 89"
Test #4:
score: 0
Accepted
time: 1020ms
memory: 40176kb
input:
21 10000000 1432782 1230128 1693282 1456826 605524 521515 2742745 3427204 2231114 2129928 2345527 2397808 511783 521160 2041234 2313965 2323807 2603481 1232121 1410811 719508 850004 416942 495559 2180169 2579591 1580089 1786914 2317568 2292171 1514260 1143717 1348703 1495001 562052 525544 2818854 23...
output:
9336572 5
result:
ok 2 number(s): "9336572 5"
Test #5:
score: 0
Accepted
time: 2653ms
memory: 54904kb
input:
22 10000000 1562592 1176882 1693226 1513484 2293770 2757728 2612851 3010518 1971354 2392268 2475363 2035487 641627 684375 2171036 2181775 1544541 1633457 1361981 1060447 2277948 2792254 157192 141039 1011327 1139897 541119 577682 1538276 1451191 2423314 2061841 1088919 1154927 42526 43789 1779858 16...
output:
8019829 516
result:
ok 2 number(s): "8019829 516"
Test #6:
score: -100
Wrong Answer
time: 2268ms
memory: 55156kb
input:
22 50000000 9900000 50000000 9800000 50000000 9700000 50000000 9600000 50000000 9500000 50000000 9400000 50000000 9300000 50000000 9200000 50000000 9100000 50000000 9190000 50000000 9180000 50000000 9170000 50000000 9160000 50000000 9150000 50000000 9140000 50000000 9130000 50000000 9120000 50000000...
output:
200000000 67370457
result:
wrong answer 1st numbers differ - expected: '250000000', found: '200000000'