UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212284#3813. T3drdilyor603305ms194796kbC++112.7kb2024-10-13 17:38:342024-10-13 19:38:51

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
void ts(){cout<<"IAKIOI\n";}
inline int read(){
	int n=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		n=n*10+ch-'0';
		ch=getchar();
	}
	return n*f;
}
bool Mbe;
int n;
int a[200005];
vector<int> e[200005];
vector<int> dp[2005][2005];
//每次交换就是二叉树上换一条边.
//i 的子树一开始 a[i]=j. 最小的 bfs 序.
vector<int> combi(vector<int> a,vector<int> b){
	int now=1;
	vector<int> res;
	int pa=0,pb=0;
	while(1){
		if(pa>=(int)a.size()&&pb>=(int)b.size())break;
		if(pa+now-1<(int)a.size()&&pb+now-1<(int)b.size()){
			for(int i=pa;i<pa+now;i++)res.push_back(a[i]);
			for(int i=pb;i<pb+now;i++)res.push_back(b[i]);
			pa+=now,pb+=now;now*=2;continue;
		}
		if(pa+now-1<(int)a.size()){
			for(int i=pa;i<pa+now;i++)res.push_back(a[i]);
			for(int i=pb;i<(int)b.size();i++)res.push_back(b[i]);
			pb=(int)b.size();pa+=now;now*=2;continue;
		}	
		if(pb+now-1<(int)b.size()){
			for(int i=pa;i<(int)a.size();i++)res.push_back(a[i]);
			for(int i=pb;i<pb+now;i++)res.push_back(b[i]);
			pa=(int)a.size();pb+=now;now*=2;continue;
		}		
		for(int i=pa;i<(int)a.size();i++)res.push_back(a[i]);
		for(int i=pb;i<(int)b.size();i++)res.push_back(b[i]);
		break;
	}
	return res;
}
bool Med;
signed main(){
	//cerr<<(&Mbe-&Med)/1048576.00<<"\n";
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=2;i<=n;i++)e[i/2].push_back(i);
	for(int i=1;i<=n;i++)sort(e[i].begin(),e[i].end());
	for(int i=n;i>=1;i--){
		for(int j=1;j<=n;j++)dp[i][j].push_back(j);
		if(e[i].empty())continue;
		if((int)e[i].size()==1){
			int s=e[i][0];
			for(int j=1;j<=n;j++){
				//现在 a[i]=j.
				dp[i][j].clear();
				if(j>a[s]){
					dp[i][j].push_back(a[s]);
					for(auto v:dp[s][j])dp[i][j].push_back(v);
				}
				else{
					dp[i][j].push_back(j);
					for(auto v:dp[s][a[s]])dp[i][j].push_back(v);
				}
			}
		}
		else{
			int ls=e[i][0],rs=e[i][1];
			for(int j=1;j<=n;j++){
				dp[i][j].clear();
				if(j<=a[ls]&&j<=a[rs]){
					dp[i][j].push_back(j);
					vector<int> tmp=combi(dp[ls][a[ls]],dp[rs][a[rs]]);
					for(auto it:tmp)dp[i][j].push_back(it);
				}
				else if(a[ls]<=j&&a[ls]<=a[rs]){
					dp[i][j].push_back(a[ls]);
					vector<int> tmp=combi(dp[ls][j],dp[rs][a[rs]]);
					for(auto it:tmp)dp[i][j].push_back(it);
				}
				else{
					dp[i][j].push_back(a[rs]);
					vector<int> ta=combi(dp[ls][a[ls]],dp[rs][j]);
					vector<int> tb=combi(dp[ls][j],dp[rs][a[ls]]);
					if(ta>tb)swap(ta,tb);
					for(auto v:ta)dp[i][j].push_back(v);
				}
			}
		}
	}
	for(auto v:dp[1][a[1]])cout<<v<<" ";
	cout<<"\n";
	return 0;
}
//look at my code
//my code is amazing

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 20
Accepted

Test #1:

score: 20
Accepted
time: 21ms
memory: 100156kb

input:

20
7 8 4 12 6 19 1 5 16 14 15 20 17 18 9 10 13 11 2 3

output:

4 6 1 5 7 8 9 10 2 3 15 20 17 18 19 12 13 11 16 14 

result:

ok 20 numbers

Test #2:

score: 0
Accepted
time: 34ms
memory: 100156kb

input:

20
3 19 17 5 8 14 12 6 15 7 9 4 10 1 20 16 18 13 11 2

output:

3 5 12 6 7 4 1 16 11 2 9 14 10 17 20 19 18 13 15 8 

result:

ok 20 numbers

Test #3:

score: 0
Accepted
time: 21ms
memory: 100152kb

input:

20
13 8 2 19 7 15 5 12 9 17 3 16 4 18 10 1 11 6 14 20

output:

2 7 5 8 3 4 10 1 6 17 19 13 16 15 18 12 11 9 14 20 

result:

ok 20 numbers

Test #4:

score: 0
Accepted
time: 26ms
memory: 100156kb

input:

20
20 19 8 17 11 10 14 7 12 16 15 9 18 13 6 5 4 3 2 1

output:

8 11 10 7 15 9 6 4 2 1 16 19 18 13 14 5 17 3 12 20 

result:

ok 20 numbers

Test #5:

score: 0
Accepted
time: 20ms
memory: 100152kb

input:

20
7 19 18 3 16 15 14 13 20 11 10 9 8 12 6 5 4 2 17 1

output:

7 3 14 13 10 8 6 4 2 1 11 9 15 12 18 5 19 20 17 16 

result:

ok 20 numbers

Subtask #2:

score: 20
Accepted

Test #6:

score: 20
Accepted
time: 22ms
memory: 100228kb

input:

40
19 15 20 27 16 11 34 5 21 6 30 3 40 39 12 2 7 13 1 23 37 32 18 28 25 17 33 31 29 36 4 22 10 35 8 ...

output:

15 16 11 5 6 3 12 2 1 19 18 20 17 29 4 10 7 9 13 23 37 30 32 28 25 40 33 31 34 36 39 22 27 35 8 14 2...

result:

ok 40 numbers

Test #7:

score: 0
Accepted
time: 24ms
memory: 100232kb

input:

40
21 31 14 28 6 5 16 20 27 29 18 24 12 32 39 1 19 36 3 37 15 22 33 4 13 11 10 26 9 17 2 30 25 34 8 ...

output:

14 6 5 20 18 12 16 1 3 15 21 4 10 9 2 25 8 7 27 23 29 22 33 24 13 11 31 26 32 17 39 28 30 19 34 35 3...

result:

ok 40 numbers

Test #8:

score: 0
Accepted
time: 35ms
memory: 100232kb

input:

40
25 20 15 10 34 24 21 6 31 36 27 30 18 38 1 28 22 39 33 19 23 5 40 35 14 2 17 4 3 7 37 32 13 16 11...

output:

15 10 20 6 27 18 1 22 31 19 5 14 2 3 7 13 11 26 8 12 23 34 40 24 35 30 17 4 21 38 37 25 32 16 28 39 ...

result:

ok 40 numbers

Test #9:

score: 0
Accepted
time: 34ms
memory: 100232kb

input:

40
40 39 12 37 36 35 34 33 25 31 8 29 28 27 26 32 18 23 22 21 9 19 24 17 16 15 14 13 11 38 10 20 30 ...

output:

12 36 34 25 8 28 26 18 22 9 19 16 14 11 10 20 6 4 2 1 21 31 24 17 29 15 35 13 27 38 39 32 30 7 33 5 ...

result:

ok 40 numbers

Test #10:

score: 0
Accepted
time: 21ms
memory: 100232kb

input:

40
25 39 38 37 36 35 34 33 28 31 30 29 32 27 26 19 4 23 22 12 20 40 18 17 16 15 14 11 21 13 10 9 8 7...

output:

25 36 34 28 30 29 26 4 22 12 18 16 14 11 10 8 6 5 2 1 20 31 40 17 35 15 32 27 21 13 38 9 19 7 33 23 ...

result:

ok 40 numbers

Subtask #3:

score: 20
Accepted

Test #11:

score: 20
Accepted
time: 623ms
memory: 194784kb

input:

1000
162 57 979 220 75 121 744 693 173 262 399 558 295 360 530 363 977 552 398 101 137 946 85 712 71...

output:

57 75 121 162 220 295 360 363 173 101 85 558 419 242 46 106 238 552 58 262 137 125 35 40 10 318 11 1...

result:

ok 1000 numbers

Test #12:

score: 0
Accepted
time: 610ms
memory: 194796kb

input:

1000
691 889 435 777 119 983 387 437 963 812 398 541 529 672 227 532 901 742 103 652 382 781 433 567...

output:

435 119 387 437 398 529 227 532 103 382 433 541 214 359 312 203 193 146 257 262 396 553 691 317 60 4...

result:

ok 1000 numbers

Test #13:

score: 0
Accepted
time: 614ms
memory: 194776kb

input:

1000
160 238 245 889 848 432 354 407 719 410 557 180 827 29 579 664 812 214 496 251 944 417 186 248 ...

output:

160 238 245 407 410 180 29 664 214 251 186 248 196 354 72 434 305 179 324 144 101 417 469 432 113 42...

result:

ok 1000 numbers

Test #14:

score: 0
Accepted
time: 613ms
memory: 194796kb

input:

1000
1000 999 998 997 996 995 994 993 992 991 990 989 988 987 916 985 984 983 982 986 133 979 978 97...

output:

998 996 994 992 990 988 916 984 982 133 978 976 974 972 223 968 966 920 962 960 958 956 954 952 950 ...

result:

ok 1000 numbers

Test #15:

score: 0
Accepted
time: 587ms
memory: 194796kb

input:

1000
1000 999 998 997 996 995 463 993 992 846 411 989 988 987 986 985 984 983 982 981 806 979 978 97...

output:

998 996 463 992 411 988 986 984 982 806 846 976 974 972 970 968 966 964 962 960 958 956 954 952 950 ...

result:

ok 1000 numbers

Subtask #4:

score: 0
Runtime Error

Test #16:

score: 0
Runtime Error

input:

50000
6014 4637 42967 25211 33048 39571 24304 15155 17748 38901 41356 27235 3880 27971 22842 37080 4...

output:


result:


Subtask #5:

score: 0
Runtime Error

Test #21:

score: 0
Runtime Error

input:

200000
113806 52130 91627 30299 76623 124589 163145 143300 2089 189894 32321 116979 54734 199544 128...

output:


result: