Submission #487512

# Submission time Handle Problem Language Result Execution time Memory
487512 2021-11-15T18:09:48 Z Koosha_mv Swap (BOI16_swap) C++14
100 / 100
102 ms 31888 KB
#include <bits/stdc++.h>
using namespace std;
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define print(v,r) f(i,0,r) cout<<v[i]<<" "; cout<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define Add(x,y) x=(x+y)%mod
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N=1e6+99;

int n,t,s[N],a[N],ans[N];
vector<int> g[N];

void Min(int &id,int x){
	if(a[x]<a[id]) id=x;
}
bool is_par(int u,int v){
	while(u<v) v/=2;
	return (u==v);
}

int main(){
	fill(s,s+N,-1);
	a[0]=N;
	cin>>n;
	f(i,1,n+1){
		cin>>a[i];
	}
	f(i,1,n+1){
		int id=0,u=i;
		if(s[i]!=1) id=i;
		if(i*2<=n) Min(id,i*2+0);
		if(i*2<n) Min(id,i*2+1);
		
		while(u>1 && s[u]!=0){
			if((u&1) && s[u^1]!=0){
				Min(id,u^1);
			}
			if(s[u/2]!=1 && (u%2==0 || s[u^1]!=1)){
				Min(id,u/2);
			}
			if((u&1) && s[u^1]==1){
				break;
			}
			u/=2;
		}
		
		if(id==i*2){
			s[i*2+0]=1;
			s[i*2+1]=0;
		}
		else if(id==i*2+1){
			s[i*2+1]=1;
		}
		else if(i==id){
			s[i]=0;
			s[i*2+0]=0;
			s[i*2+1]=0;
		}
		else if(is_par(id,i)){
			int lastu=0;
			s[id]=0;
			s[i*2+0]=0;
			s[i*2+1]=0;
			u=i;
			while(u!=id){
				s[u]=1;
				if((u&1)){
					s[u^1]=0;
				}
				lastu=u;
				u/=2;
			}
			if(lastu&1){
				s[lastu^1]=0;
			}
		}
		else{
			s[i*2+0]=0;
			s[i*2+1]=0;
			u=i;
			s[id]=1;
			while(u>1 && u*2!=id){
				s[u]=1;
				if((u^1)!=id && (u&1)){
					s[u^1]=0;
				}
				u/=2;
				if(u==0) exit(0);
			}
		}
		ans[i]=a[id];
	}
	f(i,1,n+1){
		cout<<ans[i]<<" ";
	}
}
/*
8
5 1 4 6 2 8 7 3

1 2 4 3 5 8 7 6
*/
# Verdict Execution time Memory Grader output
1 Correct 13 ms 27724 KB Output is correct
2 Correct 13 ms 27596 KB Output is correct
3 Correct 15 ms 27596 KB Output is correct
4 Correct 15 ms 27712 KB Output is correct
5 Correct 12 ms 27596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 27724 KB Output is correct
2 Correct 13 ms 27596 KB Output is correct
3 Correct 15 ms 27596 KB Output is correct
4 Correct 15 ms 27712 KB Output is correct
5 Correct 12 ms 27596 KB Output is correct
6 Correct 13 ms 27712 KB Output is correct
7 Correct 14 ms 27700 KB Output is correct
8 Correct 13 ms 27600 KB Output is correct
9 Correct 13 ms 27600 KB Output is correct
10 Correct 15 ms 27648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 27724 KB Output is correct
2 Correct 13 ms 27596 KB Output is correct
3 Correct 15 ms 27596 KB Output is correct
4 Correct 15 ms 27712 KB Output is correct
5 Correct 12 ms 27596 KB Output is correct
6 Correct 13 ms 27712 KB Output is correct
7 Correct 14 ms 27700 KB Output is correct
8 Correct 13 ms 27600 KB Output is correct
9 Correct 13 ms 27600 KB Output is correct
10 Correct 15 ms 27648 KB Output is correct
11 Correct 16 ms 27660 KB Output is correct
12 Correct 15 ms 27616 KB Output is correct
13 Correct 17 ms 27696 KB Output is correct
14 Correct 15 ms 27660 KB Output is correct
15 Correct 14 ms 27728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 27724 KB Output is correct
2 Correct 13 ms 27596 KB Output is correct
3 Correct 15 ms 27596 KB Output is correct
4 Correct 15 ms 27712 KB Output is correct
5 Correct 12 ms 27596 KB Output is correct
6 Correct 13 ms 27712 KB Output is correct
7 Correct 14 ms 27700 KB Output is correct
8 Correct 13 ms 27600 KB Output is correct
9 Correct 13 ms 27600 KB Output is correct
10 Correct 15 ms 27648 KB Output is correct
11 Correct 16 ms 27660 KB Output is correct
12 Correct 15 ms 27616 KB Output is correct
13 Correct 17 ms 27696 KB Output is correct
14 Correct 15 ms 27660 KB Output is correct
15 Correct 14 ms 27728 KB Output is correct
16 Correct 31 ms 28604 KB Output is correct
17 Correct 30 ms 28672 KB Output is correct
18 Correct 31 ms 28624 KB Output is correct
19 Correct 36 ms 28660 KB Output is correct
20 Correct 37 ms 28712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 27724 KB Output is correct
2 Correct 13 ms 27596 KB Output is correct
3 Correct 15 ms 27596 KB Output is correct
4 Correct 15 ms 27712 KB Output is correct
5 Correct 12 ms 27596 KB Output is correct
6 Correct 13 ms 27712 KB Output is correct
7 Correct 14 ms 27700 KB Output is correct
8 Correct 13 ms 27600 KB Output is correct
9 Correct 13 ms 27600 KB Output is correct
10 Correct 15 ms 27648 KB Output is correct
11 Correct 16 ms 27660 KB Output is correct
12 Correct 15 ms 27616 KB Output is correct
13 Correct 17 ms 27696 KB Output is correct
14 Correct 15 ms 27660 KB Output is correct
15 Correct 14 ms 27728 KB Output is correct
16 Correct 31 ms 28604 KB Output is correct
17 Correct 30 ms 28672 KB Output is correct
18 Correct 31 ms 28624 KB Output is correct
19 Correct 36 ms 28660 KB Output is correct
20 Correct 37 ms 28712 KB Output is correct
21 Correct 86 ms 31704 KB Output is correct
22 Correct 86 ms 31816 KB Output is correct
23 Correct 89 ms 31716 KB Output is correct
24 Correct 102 ms 31816 KB Output is correct
25 Correct 98 ms 31888 KB Output is correct