QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#625904 | #8005. Crossing the Border | pbk5418 | WA | 128ms | 28028kb | C++14 | 3.2kb | 2024-10-09 21:39:49 | 2024-10-09 21:39:49 |
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() {
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);
reverse(b.begin(),b.end());
int j = 0,F = 0x3f3f3f3f;
for (int X : b) {
int ss = W - 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 ++;
}
int to = (X << nn) + Y;
f[to] = min(f[to],F);
}
}
}
for (int x = 1; 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);
reverse(b.begin(),b.end());
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]);
}
/*
31 28 9 1
31 13 9 1
31 5 9 1
31 9 9 1
31 17 9 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22352kb
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: -100
Wrong Answer
time: 128ms
memory: 28028kb
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:
7986645 10041
result:
wrong answer 1st numbers differ - expected: '9391997', found: '7986645'