Submission #46815

#TimeUsernameProblemLanguageResultExecution timeMemory
46815BruteforcemanSwap (BOI16_swap)C++11
0 / 100
3 ms1912 KiB
#include "bits/stdc++.h"
using namespace std;
int a[100010 << 2];
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;
}

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]);
	}

	priority_queue <int, vector <int>, greater <int>> PQ;
	queue <int> Q;
	Q.push(1);
	while(!Q.empty()) {
		int x = Q.front();
		Q.pop();
		int opt = PQ.empty() ? inf : PQ.top();
		int f = func(x);
		int l = x << 1;
		int r = l + 1;

		if(a[x] == inf) {
			if(opt < f) {
				a[x] = opt;
				PQ.pop();
			} else if (a[r] == f) {
				swap(a[x], a[r]);
				PQ.push(a[l]);
				a[l] = inf;
			} else {
				swap(a[x], a[l]);
			}
		} else {
			if(a[r] == f) {
				PQ.push(a[x]);
				PQ.push(a[l]);
				a[x] = a[r];
				a[l] = a[r] = inf;
			} else if (a[l] == f) {
				swap(a[x], a[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;
}

Compilation message (stderr)

swap.cpp: In function 'int main(int, const char**)':
swap.cpp:18:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
swap.cpp:20: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...