Submission #259548

# Submission time Handle Problem Language Result Execution time Memory
259548 2020-08-08T02:18:03 Z super_j6 Swap (BOI16_swap) C++14
100 / 100
64 ms 6848 KB
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second

const int mxn = 400001;
int n;
int a[mxn], f[mxn], ans[mxn];

void add(int x, int v){
	for(; x; x >>= 1){
		if(a[x] == v){
			f[x] = 0;
			break;
		}
		if(x & 1){
			if(a[x ^ 1] == v){
				f[x] = f[x ^ 1] = 1;
				break;
			}
			f[x ^ 1] = 0;
		}
		f[x] = 1;
	}
}

int qry(int x){
	int ret = mxn;
	for(; x; x >>= 1){
		if(~-f[x]) ret = min(ret, a[x]);
		if(!f[x]) break;
		if((x & 1) && f[x ^ 1]){
			ret = min(ret, a[x ^ 1]);
			if(!~-f[x ^ 1]) break;
		}
	}
	return ret;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	
	memset(a, 0x3f, sizeof(a));
	memset(f, -1, sizeof(f));
	for(int i = 1; i <= n; i++) cin >> a[i];
	f[1] = 0;
	
	for(int i = 1; i <= n; i++){
		int x = qry(i), l = a[i << 1], r = a[i << 1 | 1];
		ans[i] = min({x, l, r});
		if(ans[i] == x){
			f[i << 1] = f[i << 1 | 1] = 0;
			add(i, x);
		}
		if(ans[i] == l){
			f[i << 1] = 1, f[i << 1 | 1] = 0;
		}
		if(ans[i] == r){
			f[i << 1 | 1] = 1;
		}
	}
	
	cout << ans[1];
	for(int i = 2; i <= n; i++) cout << " " << ans[i];
	cout << endl;
 
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Correct 2 ms 3456 KB Output is correct
4 Correct 2 ms 3456 KB Output is correct
5 Correct 2 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Correct 2 ms 3456 KB Output is correct
4 Correct 2 ms 3456 KB Output is correct
5 Correct 2 ms 3456 KB Output is correct
6 Correct 2 ms 3456 KB Output is correct
7 Correct 2 ms 3456 KB Output is correct
8 Correct 2 ms 3456 KB Output is correct
9 Correct 2 ms 3456 KB Output is correct
10 Correct 2 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Correct 2 ms 3456 KB Output is correct
4 Correct 2 ms 3456 KB Output is correct
5 Correct 2 ms 3456 KB Output is correct
6 Correct 2 ms 3456 KB Output is correct
7 Correct 2 ms 3456 KB Output is correct
8 Correct 2 ms 3456 KB Output is correct
9 Correct 2 ms 3456 KB Output is correct
10 Correct 2 ms 3456 KB Output is correct
11 Correct 2 ms 3456 KB Output is correct
12 Correct 2 ms 3456 KB Output is correct
13 Correct 2 ms 3456 KB Output is correct
14 Correct 2 ms 3456 KB Output is correct
15 Correct 2 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Correct 2 ms 3456 KB Output is correct
4 Correct 2 ms 3456 KB Output is correct
5 Correct 2 ms 3456 KB Output is correct
6 Correct 2 ms 3456 KB Output is correct
7 Correct 2 ms 3456 KB Output is correct
8 Correct 2 ms 3456 KB Output is correct
9 Correct 2 ms 3456 KB Output is correct
10 Correct 2 ms 3456 KB Output is correct
11 Correct 2 ms 3456 KB Output is correct
12 Correct 2 ms 3456 KB Output is correct
13 Correct 2 ms 3456 KB Output is correct
14 Correct 2 ms 3456 KB Output is correct
15 Correct 2 ms 3456 KB Output is correct
16 Correct 14 ms 4228 KB Output is correct
17 Correct 14 ms 4352 KB Output is correct
18 Correct 14 ms 4224 KB Output is correct
19 Correct 17 ms 4224 KB Output is correct
20 Correct 17 ms 4224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Correct 2 ms 3456 KB Output is correct
4 Correct 2 ms 3456 KB Output is correct
5 Correct 2 ms 3456 KB Output is correct
6 Correct 2 ms 3456 KB Output is correct
7 Correct 2 ms 3456 KB Output is correct
8 Correct 2 ms 3456 KB Output is correct
9 Correct 2 ms 3456 KB Output is correct
10 Correct 2 ms 3456 KB Output is correct
11 Correct 2 ms 3456 KB Output is correct
12 Correct 2 ms 3456 KB Output is correct
13 Correct 2 ms 3456 KB Output is correct
14 Correct 2 ms 3456 KB Output is correct
15 Correct 2 ms 3456 KB Output is correct
16 Correct 14 ms 4228 KB Output is correct
17 Correct 14 ms 4352 KB Output is correct
18 Correct 14 ms 4224 KB Output is correct
19 Correct 17 ms 4224 KB Output is correct
20 Correct 17 ms 4224 KB Output is correct
21 Correct 53 ms 6848 KB Output is correct
22 Correct 54 ms 6776 KB Output is correct
23 Correct 54 ms 6776 KB Output is correct
24 Correct 64 ms 6800 KB Output is correct
25 Correct 63 ms 6776 KB Output is correct