QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#626716 | #8005. Crossing the Border | pbk5418 | TL | 591ms | 24252kb | C++14 | 3.3kb | 2024-10-10 11:56:55 | 2024-10-10 11:56:55 |
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;
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 ++)
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]);
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 ++) {
b.clear();
for (int X = x; X < R; X = (X + 1) | x) b.push_back(X);
sort(b.begin(),b.end(),[&](int A,int B){return wt[(A - x) << nn] > wt[(B - x) << nn];});
int j = 0,F = 0x3f3f3f3f;
for (int X : b) {
int ss = W - wt[(X - x) << nn],to = (X << nn) + Y;
// cout << wt[(X - x) << nn] << " ";
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);
}
// puts("");
}
}
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 ++) {
b.clear();
for (int X = x; X < R; X = (X + 1) | x) b.push_back(X);
sort(b.begin(),b.end(),[&](int A,int B){return wt[(A - x) << nn] > wt[(B - x) << nn];});
int j = 0,F = 0x3f3f3f3f,G = 0;
for (int X : b) {
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: 22300kb
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: 591ms
memory: 24252kb
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: -100
Time Limit Exceeded
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...