Submission #46827

#TimeUsernameProblemLanguageResultExecution timeMemory
46827BruteforcemanSwap (BOI16_swap)C++11
0 / 100
3 ms1912 KiB
#include "bits/stdc++.h"
using namespace std;
int a[200010 << 1];
int p[200010];
int n;
const int inf = 1e9;

int func(int x) {
	int ans = inf;
	ans = min(ans, a[x]);
	if((x << 1) <= n) ans = min(ans, a[x << 1]);
	if(((x << 1) | 1) <= n) ans = min(ans, a[(x << 1) | 1]);
	return ans;
}

bool reach(int x, int y) {
	while(x) {
		int l = x << 1;
		int r = l + 1;
		if(l == y || r == y) return true;
		if(x == y) return true;
		x >>= 1;
	}
	return false;
} 

int main(int argc, char const *argv[])
{
	memset(a, -1, sizeof a);
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		p[i] = a[i];
	}
	vector <int> v; 
	queue <int> Q;
	Q.push(1);
	while(!Q.empty()) {
		int x = Q.front();
		Q.pop();
		int f = func(x);
		int l = x << 1;
		int r = l + 1;

		if(a[x] == inf) {
			int opt = inf;
			int idx = -1;
			for(int i = 0; i < v.size(); i++) {
				if(reach(x, v[i]) && opt > p[v[i]]) {
					opt = p[v[i]];
					idx = v[i];
				}
			}
			if(opt < f) {
				a[x] = opt;
				v.erase(find(v.begin(), v.end(), idx));
			} else if (a[r] == f) {
				swap(a[x], a[r]);
				v.push_back(l);
				a[l] = inf;
			} else {
				swap(a[x], a[l]);
				swap(p[x], p[l]);
			}
		} else {
			if(a[r] == f) {
				v.push_back(l);
				v.push_back(r);
				swap(p[x], p[r]);
				swap(a[x], a[r]);
				a[l] = a[r] = inf;
			} else if (a[l] == f) {
				swap(a[x], a[l]);
				swap(p[x], p[l]);
			}
		}
		if(l <= n) Q.push(l);
		if(r <= n) Q.push(r);
	}
	for(int i = 1; i <= n; i++) {
		printf("%d ", a[i]);
	}
	printf("\n");
	return 0;
}
// 9 1 3 11 6 4 5 2 8 15 14 16 7 17 13 12 18 10 20 19
// 9 1 3 11 6 4 5 2 8 15 14 16 7 10 13 12 17 18 20 19
// 9 1 3 11 6 4 5 1 8 15 14 16 7 17 13 12 2 10 18 19

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

Compilation message (stderr)

swap.cpp: In function 'int main(int, const char**)':
swap.cpp:48:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int i = 0; i < v.size(); i++) {
                   ~~^~~~~~~~~~
swap.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
swap.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...