Submission #44695

# Submission time Handle Problem Language Result Execution time Memory
44695 2018-04-05T03:00:34 Z cheater2k Swap (BOI16_swap) C++14
0 / 100
2 ms 476 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 200005;
const int INF = 1e9;

int n, a[N];

void solve(int v) {
	int lef = (v << 1);
	int rig = (v << 1 | 1);

	if (rig > n) {
		if (a[lef] < a[v]) {
			swap(a[lef], a[v]); return;
		}
	}

	int minall = min({a[v], a[lef], a[rig]});
	if (minall == a[v]) {
		return;
	}
	else if (minall == a[lef]) {
		swap(a[lef], a[v]); return;
	}
	else { // minall = a[rig]
		// -> a[rig], a[v], a[lef]
		// -> a[rig], a[lef], a[v]
		int val[2] = {a[v], a[lef]};
		a[v] = a[rig];

		vector <int> best(4, INF + 2);
		int best_t = 0;
		for (int t = 0; t < 2; ++t) {
			// a[lef] = val[t], a[rig] = val[t ^ 1]
			int curlef = val[t];
			int currig = val[t ^ 1];
			int childlef = min( (lef * 2 <= n) ? a[lef * 2] : INF, (lef * 2 + 1 <= n) ? a[lef * 2 + 1] : INF );
			int childrig = min( (rig * 2 <= n) ? a[rig * 2] : INF + 1, (rig * 2 + 1 <= n) ? a[rig * 2 + 1] : INF + 1 );

			if (curlef > childlef) swap(curlef, childlef);
			if (currig > childrig) swap(currig, childrig);

			vector <int> cur;
			cur.push_back(curlef);
			cur.push_back(currig);
			cur.push_back(childlef);
			cur.push_back(childrig);

			if (cur < best) {
				best_t = t;
				best = cur;
			}
		}

		a[lef] = val[best_t];
		a[rig] = val[best_t ^ 1];
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);

	cin >> n;
	for (int i = 1; i <= n; ++i) cin >> a[i];

	for (int i = 1; i <= (n / 2); ++i) {
		solve(i);
	}

	for (int i = 1; i <= n; ++i) {
		printf("%d ", a[i]);
	}
	printf("\n");
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Incorrect 2 ms 476 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Incorrect 2 ms 476 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Incorrect 2 ms 476 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Incorrect 2 ms 476 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Incorrect 2 ms 476 KB Output isn't correct
4 Halted 0 ms 0 KB -