Submission #229670

# Submission time Handle Problem Language Result Execution time Memory
229670 2020-05-05T19:35:58 Z DrSwad Swap (BOI16_swap) C++17
100 / 100
101 ms 7544 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "/Users/swad/Desktop/CP/debug.h"
#endif

const int N = int(2e5) + 10;

int n;
int a[N << 1];
int at[N << 1];
int to[N << 1];
int vis[N << 1];

void setVis(int at, int val) {
	if (vis[at]) return;
	vis[at] = val;

	while (at) {
		if (vis[at << 1] == 1) to[at] = to[at << 1];
		else if (vis[at << 1 | 1] != 1) to[at] = at;
		else if (vis[at << 1] == -1) to[at] = to[at << 1 | 1];
		else to[at] = min(to[at << 1], to[at << 1 | 1]);
		at >>= 1;
	}
}

void cancelVis(int from) {
	int at = from;
	while (at < to[from]) {
		if (vis[at << 1] == 1) at <<= 1;
		else if (vis[at << 1] == -1) (at <<= 1) |= 1;
		else {
			if (to[at << 1] < to[at << 1 | 1]) setVis(at << 1, 1), at <<= 1;
			else setVis(at << 1, -1), (at <<= 1) |= 1;
		}
	}
	if (vis[at << 1] != -1) setVis(at << 1, -1);
	if (vis[at << 1 | 1] != -1) setVis(at << 1 | 1, -1);
}

int main() {
	#ifdef LOCAL
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
	#endif

	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		at[a[i]] = i;
	}

	iota(to, to + (N << 1), 0);
	memset(&vis[n + 1], -1, sizeof(int) * ((N << 1) - (n + 1)));

	setVis(1, -1);

	for (int i = 1; i <= n; i++) {
		if (vis[at[i]] != -1) {
			if (at[i] & 1) setVis(at[i], 1);
			else {
				if (vis[at[i] + 1] != 1) setVis(at[i], 1), setVis(at[i] + 1, -1);
				else if (vis[at[i]] == 1) cancelVis(at[i] + 1);
				else {
					if (to[at[i]] < to[at[i] + 1]) {
						cancelVis(at[i]);
						setVis(at[i], -1);
					}
					else {
						cancelVis(at[i] + 1);
						setVis(at[i], 1);
					}
				}
			}
		}
		else cancelVis(at[i]);
	}

	for (int i = 2; i <= n; i++) if (vis[i] == 1) swap(a[i], a[i >> 1]);

	for (int i = 1; i <= n; i++) {
		if (i > 1) printf(" ");
		printf("%d", a[i]);
	}
	puts("");

	return 0;
}

Compilation message

swap.cpp: In function 'int main()':
swap.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
swap.cpp:52:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3456 KB Output is correct
2 Correct 6 ms 3456 KB Output is correct
3 Correct 6 ms 3456 KB Output is correct
4 Correct 6 ms 3456 KB Output is correct
5 Correct 7 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3456 KB Output is correct
2 Correct 6 ms 3456 KB Output is correct
3 Correct 6 ms 3456 KB Output is correct
4 Correct 6 ms 3456 KB Output is correct
5 Correct 7 ms 3456 KB Output is correct
6 Correct 6 ms 3456 KB Output is correct
7 Correct 6 ms 3456 KB Output is correct
8 Correct 6 ms 3456 KB Output is correct
9 Correct 6 ms 3456 KB Output is correct
10 Correct 6 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3456 KB Output is correct
2 Correct 6 ms 3456 KB Output is correct
3 Correct 6 ms 3456 KB Output is correct
4 Correct 6 ms 3456 KB Output is correct
5 Correct 7 ms 3456 KB Output is correct
6 Correct 6 ms 3456 KB Output is correct
7 Correct 6 ms 3456 KB Output is correct
8 Correct 6 ms 3456 KB Output is correct
9 Correct 6 ms 3456 KB Output is correct
10 Correct 6 ms 3456 KB Output is correct
11 Correct 7 ms 3456 KB Output is correct
12 Correct 7 ms 3456 KB Output is correct
13 Correct 7 ms 3456 KB Output is correct
14 Correct 7 ms 3456 KB Output is correct
15 Correct 7 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3456 KB Output is correct
2 Correct 6 ms 3456 KB Output is correct
3 Correct 6 ms 3456 KB Output is correct
4 Correct 6 ms 3456 KB Output is correct
5 Correct 7 ms 3456 KB Output is correct
6 Correct 6 ms 3456 KB Output is correct
7 Correct 6 ms 3456 KB Output is correct
8 Correct 6 ms 3456 KB Output is correct
9 Correct 6 ms 3456 KB Output is correct
10 Correct 6 ms 3456 KB Output is correct
11 Correct 7 ms 3456 KB Output is correct
12 Correct 7 ms 3456 KB Output is correct
13 Correct 7 ms 3456 KB Output is correct
14 Correct 7 ms 3456 KB Output is correct
15 Correct 7 ms 3456 KB Output is correct
16 Correct 27 ms 4472 KB Output is correct
17 Correct 27 ms 4472 KB Output is correct
18 Correct 26 ms 4480 KB Output is correct
19 Correct 21 ms 4480 KB Output is correct
20 Correct 21 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3456 KB Output is correct
2 Correct 6 ms 3456 KB Output is correct
3 Correct 6 ms 3456 KB Output is correct
4 Correct 6 ms 3456 KB Output is correct
5 Correct 7 ms 3456 KB Output is correct
6 Correct 6 ms 3456 KB Output is correct
7 Correct 6 ms 3456 KB Output is correct
8 Correct 6 ms 3456 KB Output is correct
9 Correct 6 ms 3456 KB Output is correct
10 Correct 6 ms 3456 KB Output is correct
11 Correct 7 ms 3456 KB Output is correct
12 Correct 7 ms 3456 KB Output is correct
13 Correct 7 ms 3456 KB Output is correct
14 Correct 7 ms 3456 KB Output is correct
15 Correct 7 ms 3456 KB Output is correct
16 Correct 27 ms 4472 KB Output is correct
17 Correct 27 ms 4472 KB Output is correct
18 Correct 26 ms 4480 KB Output is correct
19 Correct 21 ms 4480 KB Output is correct
20 Correct 21 ms 4472 KB Output is correct
21 Correct 95 ms 7544 KB Output is correct
22 Correct 101 ms 7544 KB Output is correct
23 Correct 94 ms 7520 KB Output is correct
24 Correct 66 ms 7544 KB Output is correct
25 Correct 69 ms 7544 KB Output is correct