QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#403644 | #6742. Leaves | CCF_NOI# | WA | 1ms | 5920kb | C++17 | 2.1kb | 2024-05-02 16:17:52 | 2024-05-02 16:17:53 |
Judging History
answer
#include<iostream>
#include<cassert>
#include<vector>
#include<cstdio>
#include<set>
using namespace std;
#define For(i,l,r) for(int i=l;i<=r;i++)
#define FOR(i,r,l) for(int i=r;i>=l;i--)
#define ll long long
#define N 1005
int n,m,ls[N],rs[N],val[N],siz[N],lim[N],f[N][N],g[N][N];
ll get()
{
ll x;
scanf("%lld",&x);
return x;
}
void getlist(int u,int sum,int swp,int lscnt,int* a,int& start)
{
if(!ls[u])
{
a[start++]=val[u];
return;
}
int rscnt=sum-lscnt-swp;
if(!swp)
{
getlist(ls[u],lscnt,f[ls[u]][lscnt],g[ls[u]][lscnt],a,start);
getlist(rs[u],rscnt,f[rs[u]][rscnt],g[rs[u]][rscnt],a,start);
}
else
{
getlist(rs[u],rscnt,f[rs[u]][rscnt],g[rs[u]][rscnt],a,start);
getlist(ls[u],lscnt,f[ls[u]][lscnt],g[ls[u]][lscnt],a,start);
}
}
bool check(int u,int sum,int swp1,int lscnt1,int swp2,int lscnt2)
{
int* a=new int[siz[u]];
int* b=new int[siz[u]];
int cnt1=0,cnt2=0;
getlist(u,sum,swp1,lscnt1,a,cnt1);
getlist(u,sum,swp2,lscnt2,b,cnt2);
assert(cnt1==siz[u]&&cnt2==siz[u]);
For(i,0,siz[u]-1)
{
if(a[i]>b[i])
{
delete[] a;
delete[] b;
return 1;
}
if(a[i]<b[i])
{
delete[] a;
delete[] b;
return 0;
}
}
delete[] a;
delete[] b;
return 0;
}
void dfs(int u)
{
if(!ls[u])
{
f[u][0]=0;
siz[u]=1;
return;
}
dfs(ls[u]);
dfs(rs[u]);
lim[u]=lim[ls[u]]+lim[rs[u]]+1;
siz[u]=siz[ls[u]]+siz[rs[u]];
For(i,0,min(m,lim[ls[u]]))For(j,0,min(m-i,lim[rs[u]]))if(f[ls[u]][i]!=-1&&f[rs[u]][j]!=-1)
{
if(f[u][i+j]==-1)f[u][i+j]=0,g[u][i+j]=i;
else if(check(u,i+j,f[u][i+j],g[u][i+j],0,i))//前面的大于后面的
{
f[u][i+j]=0,g[u][i+j]=i;
}
if(i+j+1>m)continue;
if(f[u][i+j+1]==-1)f[u][i+j+1]=1,g[u][i+j+1]=i;
else if(check(u,i+j+1,f[u][i+j+1],g[u][i+j+1],1,i))
{
f[u][i+j+1]=1,g[u][i+j+1]=i;
}
}
}
int main()
{
n=get(),m=get();
For(i,1,n)
{
int type=get();
if(type==1)ls[i]=get(),rs[i]=get();
else val[i]=get();
}
For(i,1,n)For(j,0,n)f[i][j]=-1;
dfs(1);
int* a=new int[siz[1]];
int cnt=0;
getlist(1,m,f[1][m],g[1][m],a,cnt);
For(i,0,cnt-1)cout<<a[i]<<" \n"[i==cnt-1];
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5740kb
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: 1ms
memory: 5820kb
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: 0ms
memory: 5920kb
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: 5764kb
input:
1 0 2 1000000000
output:
1000000000
result:
ok 1 number(s): "1000000000"
Test #5:
score: 0
Accepted
time: 1ms
memory: 5700kb
input:
3 1 1 2 3 2 1 2 2
output:
2 1
result:
ok 2 number(s): "2 1"
Test #6:
score: -100
Wrong Answer
time: 1ms
memory: 5920kb
input:
7 2 1 2 3 1 4 5 1 6 7 2 1 2 2 2 3 2 4
output:
2 1 4 3
result:
wrong answer 1st numbers differ - expected: '1', found: '2'