QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#411942 | #6742. Leaves | Ynoiynoi# | ML | 7ms | 22100kb | C++14 | 2.8kb | 2024-05-15 21:58:33 | 2024-05-15 21:58:34 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define MAXN 1005
#define int long long
const int INF = 1e18;
int n,m;
int a[MAXN];
struct aa {
int a[MAXN],nn;
void wt() {
for(int i = 1; i <= nn; i ++)
cout<<a[i]<<" ";
cout<<"\n";
}
};
aa merge(aa a,aa b) {
aa c;
c.nn = 0;
c.nn = a.nn+b.nn;
for(int i = 1; i <= a.nn; i ++)
c.a[i] = a.a[i];
for(int i = 1; i <= b.nn; i ++)
c.a[i+a.nn] = b.a[i];
return c;
}
bool operator <(aa a,aa b) {
for(int i = 1; i <= a.nn; i ++) {
if(a.a[i] > b.a[i]) return 0;
if(a.a[i] < b.a[i]) return 1;
}
return 0;
}
aa f[MAXN],b[MAXN];
int c[MAXN],zw[MAXN];
int lc[MAXN],rc[MAXN];
void dfs(int x) {
// cout<<x<<"\n";
if(a[x] != 0) {
f[x].nn = 1;
f[x].a[1] = a[x];
b[x] = f[x];
return;
}
dfs(lc[x]);
dfs(rc[x]);
b[x] = merge(b[lc[x]],b[rc[x]]);
c[x] = c[lc[x]]+c[rc[x]];
aa f1 = merge(f[lc[x]],f[rc[x]]);
aa f2 = merge(f[rc[x]],f[lc[x]]);
zw[x] = zw[lc[x]]+zw[rc[x]];
if(f1 < f2) {
f[x] = f1;
return;
} else {
f[x] = f2;
if(f2 < f1) {
c[x] ++; zw[x] ++;
} else zw[x] = -INF;
return;
}
}
int rt;
int ew[MAXN];
aa p[MAXN];
map<int,aa>pp[MAXN];
aa orz(int x,int m,int &zp) {
//cout<<x<<" "<<m<<"\n";
if(pp[x].count(m)) return pp[x][m];
if(a[x] != 0) return b[x];
if(m == 0) {
return b[x];
}
// if(m >= c[x]) return f[x];
aa f1,f2;
bool d1 = 0,d2 = 0;
int z11 = 0,z12 = 0,z21 = 0,z22 = 0;
if(c[lc[x]] <= m) {
d1 = 1; f1 = merge(f[lc[x]],orz(rc[x],m-c[lc[x]],z21));
} else f1 = merge(orz(lc[x],m,z12),b[rc[x]]);
if(c[rc[x]] <= m-1) {
d2 = 1; f2 = merge(f[rc[x]],orz(lc[x],m-1-c[lc[x]],z11));
} else f2 = merge(orz(rc[x],m-1,z22),b[lc[x]]);
/* cout<<x<<":----\n";
f1.wt();
f2.wt();
cout<<d1<<" "<<d2<<"?\n";*/
if(f1 < f2) {
if(d1) zp = zw[lc[x]] + z21;
else zp = z12;
pp[x][m] = f1;
// cout<<x<<" "<<m<<" "<<zp<<"\n";
return f1;
} else {
if(f2 < f1) {
if(d2) zp = z11+zw[rc[x]]+1;
else zp = z22+1;
} else zp = -INF;
pp[x][m] = f2;
//cout<<x<<" "<<m<<" "<<zp<<"??\n";
return f2;
}
}
int cc[MAXN];
bool eq(aa a,aa b) {
for(int i = 1; i <= a.nn; i ++)
if(a.a[i] != b.a[i]) return 0;
return 1;
}
aa an;
aa w[MAXN];
signed main() {
cin >> n >> m;
for(int i = 1; i <= n; i ++) {
int op;
cin >> op;
if(op == 1) {
cin >> lc[i] >> rc[i];
} else cin >> a[i];
}
dfs(1);
int zp = -1;
an = orz(1,m,zp);
/*for(int i = 1; i <= n; i ++)
cout<<zp[i]<<" ";
cout<<"\n";*/
if(zp >= 0 && zp%2 != m%2) swap(an.a[an.nn],an.a[an.nn-1]);
for(int i = 1; i <= an.nn; i ++)
cout<<an.a[i]<<" ";
return 0;
}
/*
7 1
1 2 3
1 4 5
1 6 7
2 4
2 2
2 3
2 1
3 2
1 2 3
2 2
2 1
9 2
1 2 3
1 8 9
1 4 5
1 6 7
2 20
2 30
2 1
2 3
2 3
*/
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3732kb
input:
3 0 1 2 3 2 1 2 2
output:
1 2
result:
ok 2 number(s): "1 2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3940kb
input:
7 1 1 2 3 1 4 5 1 6 7 2 4 2 2 2 3 2 1
output:
2 4 3 1
result:
ok 4 number(s): "2 4 3 1"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3924kb
input:
7 2 1 2 3 1 4 5 1 6 7 2 4 2 2 2 3 2 1
output:
1 3 4 2
result:
ok 4 number(s): "1 3 4 2"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3732kb
input:
1 0 2 1000000000
output:
1000000000
result:
ok 1 number(s): "1000000000"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3800kb
input:
3 1 1 2 3 2 1 2 2
output:
2 1
result:
ok 2 number(s): "2 1"
Test #6:
score: 0
Accepted
time: 0ms
memory: 4024kb
input:
7 2 1 2 3 1 4 5 1 6 7 2 1 2 2 2 3 2 4
output:
1 2 3 4
result:
ok 4 number(s): "1 2 3 4"
Test #7:
score: 0
Accepted
time: 3ms
memory: 22016kb
input:
999 480 1 3 2 1 4 5 1 6 7 1 9 8 1 10 11 1 13 12 1 14 15 1 16 17 1 19 18 1 21 20 1 23 22 1 25 24 1 27 26 1 28 29 1 30 31 1 33 32 1 35 34 1 37 36 1 38 39 1 41 40 1 42 43 1 45 44 1 46 47 1 48 49 1 51 50 1 52 53 1 55 54 1 56 57 1 58 59 1 61 60 1 62 63 1 64 65 1 67 66 1 69 68 1 71 70 1 73 72 1 74 75 1 76...
output:
34826804 763875883 763875883 763875883 763875883 763875883 763875883 763875883 248820103 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 7...
result:
ok 500 numbers
Test #8:
score: 0
Accepted
time: 3ms
memory: 22100kb
input:
999 480 1 2 3 1 4 5 1 7 6 1 8 9 1 10 11 1 13 12 1 15 14 1 17 16 1 19 18 1 21 20 1 23 22 1 24 25 1 26 27 1 28 29 1 31 30 1 32 33 1 34 35 1 37 36 1 38 39 1 41 40 1 42 43 1 44 45 1 46 47 1 48 49 1 51 50 1 52 53 1 54 55 1 57 56 1 58 59 1 60 61 1 62 63 1 65 64 1 66 67 1 69 68 1 71 70 1 72 73 1 75 74 1 77...
output:
530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 ...
result:
ok 500 numbers
Test #9:
score: 0
Accepted
time: 0ms
memory: 21980kb
input:
999 480 1 3 2 1 5 4 1 7 6 1 8 9 1 10 11 1 13 12 1 15 14 1 17 16 1 18 19 1 21 20 1 23 22 1 25 24 1 26 27 1 29 28 1 31 30 1 33 32 1 34 35 1 37 36 1 38 39 1 41 40 1 43 42 1 45 44 1 47 46 1 49 48 1 50 51 1 52 53 1 54 55 1 56 57 1 59 58 1 60 61 1 62 63 1 65 64 1 67 66 1 69 68 1 71 70 1 73 72 1 74 75 1 76...
output:
507922 928353918 51892154 809031414 296508656 801553730 385852006 930053625 95913518 431154803 117002192 187199343 405980569 664093190 478863675 930428963 3440725 490128679 92302056 563992614 27927487 246777697 168388113 922508131 52386784 780998040 438167217 876407248 158186938 633166175 788275543 ...
result:
ok 500 numbers
Test #10:
score: 0
Accepted
time: 7ms
memory: 21984kb
input:
999 480 1 3 2 1 4 5 1 7 6 1 8 9 1 10 11 1 13 12 1 15 14 1 16 17 1 19 18 1 21 20 1 23 22 1 24 25 1 26 27 1 29 28 1 30 31 1 32 33 1 35 34 1 37 36 1 38 39 1 41 40 1 43 42 1 45 44 1 46 47 1 48 49 1 50 51 1 52 53 1 54 55 1 56 57 1 58 59 1 60 61 1 63 62 1 65 64 1 66 67 1 68 69 1 70 71 1 72 73 1 74 75 1 76...
output:
666292 26210483 65023030 95745223 11431606 30237589 40996997 59830702 14512182 24640163 39361368 85684143 17020177 58437715 74457645 91998540 7557034 46923480 16066480 33921131 14512182 83949838 22501176 83949838 15127734 18414338 52979072 81930757 24640163 70913581 72929459 78973167 3687875 4610266...
result:
ok 500 numbers
Test #11:
score: 0
Accepted
time: 3ms
memory: 22040kb
input:
999 480 1 3 2 1 4 5 1 7 6 1 9 8 1 10 11 1 13 12 1 15 14 1 16 17 1 18 19 1 20 21 1 23 22 1 24 25 1 27 26 1 29 28 1 30 31 1 33 32 1 34 35 1 37 36 1 39 38 1 40 41 1 42 43 1 44 45 1 47 46 1 48 49 1 50 51 1 52 53 1 55 54 1 56 57 1 59 58 1 60 61 1 62 63 1 64 65 1 66 67 1 68 69 1 70 71 1 72 73 1 75 74 1 77...
output:
874376 19021636 10912872 49114704 21454471 94310772 60086613 88323254 20181141 62658098 54568372 64872279 21454471 30659919 73483837 98385133 12300384 74080822 54568372 100102768 29770805 62658098 33159023 62908008 33159023 98943188 52580720 64872279 58787422 98644936 73687255 98385133 10912872 9838...
result:
ok 500 numbers
Test #12:
score: -100
Memory Limit Exceeded
input:
905 52 1 3 2 2 25780328 1 4 5 2 84996992 1 6 7 2 43060692 1 8 9 2 58356233 1 11 10 2 48172306 1 12 13 2 94654986 1 14 15 2 66085196 1 17 16 2 43141873 1 18 19 2 58356233 1 21 20 2 58356233 1 22 23 2 64124892 1 24 25 2 55464563 1 27 26 2 46324683 1 29 28 2 78919648 1 30 31 2 13379324 1 32 33 2 960564...
output:
612429 612429 612429 19600883 48267231 95525118 49399005 95525118 64124892 49958425 57986350 11470741 64124892 52995307 91497150 29038234 78919648 41492697 43060692 8543837 49958425 13964121 95525118 67971344 46478738 8543837 17411083 57556475 43141873 56442997 66085196 68952472 67536425 15117537 70...