QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#626723 | #8005. Crossing the Border | pbk5418 | TL | 2374ms | 49236kb | C++14 | 3.1kb | 2024-10-10 12:01:56 | 2024-10-10 12:01:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int N = 22,P = 998244353;
int n,nn,W,R,S,T,mx[1 << N | 5],wt[1 << N | 5],f[1 << N | 5],g[1 << N | 5];
struct sib {int w,c;} e[31];
struct Node {int w,c,id;} ;
vector <Node> a;
vector <int> b[1 << 11 | 5];
int main() {
// freopen("sb.out","w",stdout);
scanf("%d%d",&n,&W);
for (int i = 0; i < n; i ++) scanf("%d%d",&e[i].w,&e[i].c);
sort(e,e + n,[](sib x,sib y){return x.c > y.c;});
nn = n / 2;
S = 1 << n,T = 1 << nn,R = (1 << n - nn);
memset(f,0x3f,sizeof(f));
for (int s = 1; s < S; s ++) {
int i = s & -s,j = __lg(i);
mx[s] = max(e[j].c,mx[s ^ i]);
wt[s] = wt[s ^ i] + e[j].w;
// if (wt[s] <= W) f[s] = mx[s];//,g[s] = 1;
}
f[0] = 0,g[0] = 1;
for (int x = 0; x < R; x ++) {
b[x].push_back(x);
for (int X = (x + 1) | x; X < R; X = (X + 1) | x) {
if (X - x > x && wt[(X - x) << nn] <= W)
f[X << nn] = min(f[X << nn],f[x << nn] + mx[(X - x) << nn]);
b[x].push_back(X);
}
sort(b[x].begin(),b[x].end(),[&](int A,int B){return wt[(A - x) << nn] > wt[(B - x) << nn];});
}
for (int Y = 1; Y < T; Y ++) {
a.clear();
for (int y = (Y - 1) & Y; y > 0; y = Y & (y - 1))
if (Y - y > y) a.push_back(Node{wt[Y - y],mx[Y - y],y});
a.push_back(Node{wt[Y],mx[Y],0});
sort(a.begin(),a.end(),[](Node x,Node y){return x.w < y.w;});
// printf("%d:\n",Y);
// for (Node x : a) cout << x.w << " " << x.c << " " << x.id << endl;
// cout << endl;
for (int x = 0; x < R; x ++) {
int j = 0,F = 0x3f3f3f3f;
for (int X : b[x]) {
int ss = W - wt[(X - x) << nn],to = (X << nn) + Y;
while (j < a.size() && a[j].w <= ss) {
int k = (x << nn) + a[j].id;
if (f[k] + a[j].c < F) F = f[k] + a[j].c;
j ++;
}
f[to] = min(f[to],F);
}
}
}
for (int x = 0; x < R; x ++)
for (int X = (x + 1) | x; X < R; X = (X + 1) | x)
if (X - x > x && wt[(X - x) << nn] <= W && f[x << nn] + mx[(X - x) << nn] == f[X << nn])
g[X << nn] = (g[X << nn] + g[x << nn]) % P;
// f[X << nn] = min(f[X << nn],f[x << nn] + mx[(X - x) << nn]);
// for (int i = 0; i < S; i ++)
// cout << i << " " << f[i] << " " << g[i] << endl;
for (int Y = 1; Y < T; Y ++) {
a.clear();
for (int y = (Y - 1) & Y; y > 0; y = Y & (y - 1))
if (Y - y > y) a.push_back(Node{wt[Y - y],mx[Y - y],y});
a.push_back(Node{wt[Y],mx[Y],0});
sort(a.begin(),a.end(),[](Node x,Node y){return x.w < y.w;});
// printf("%d:\n",Y);
// for (Node x : a) cout << x.w << " " << x.c << " " << x.id << endl;
// cout << endl;
for (int x = 0; x < R; x ++) {
int j = 0,F = 0x3f3f3f3f,G = 0;
for (int X : b[x]) {
int ss = W - wt[(X - x) << nn],to = (X << nn) + Y;
while (j < a.size() && a[j].w <= ss) {
int k = (x << nn) + a[j].id;
// cout << to << " " << k << " " << f[k] + a[j].c << " " << g[k] << endl;
if (f[k] + a[j].c < F) F = f[k] + a[j].c,G = 0;
if (f[k] + a[j].c == F) G = (G + g[k]) % P;
j ++;
}
if (f[to] == F) g[to] = (g[to] + G) % P;
}
}
}
// for (int i = 0; i < S; i ++)
// cout << i << " " << f[i] << " " << g[i] << endl;
printf("%d %d",f[S - 1],g[S - 1]);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22436kb
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: 106ms
memory: 24444kb
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: 810ms
memory: 34940kb
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: 2374ms
memory: 49236kb
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: -100
Time Limit Exceeded
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...