Submission #46828

# Submission time Handle Problem Language Result Execution time Memory
46828 2018-04-23T17:58:04 Z Bruteforceman Swap (BOI16_swap) C++11
0 / 100
3 ms 2076 KB
#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;

/*		cout << "v: ";
		for(auto i : v) {
			cout << i << " ";
		}
		cout << endl;
*/
		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));
				// cout << "delete " << idx << endl;
			} 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 
50
50 41 37 26 44 48 4 15 3 5 16 12 33 8 40 34 30 35 43 17 46 13 11 39 24 10 19 42 27 45 20 36 28 9 7 18 1 38 47 32 21 6 22 31 49 29 23 14 25 2 
*/
// 37 26 4 3 5 12 8 15 35 17 11 24 10 27 20 28 7 1 38 21 6 13 16 14 2 33 19 41 42 40 45 34 36 9 30 18 50 43 47 32 44 46 22 31 49 29 23 39 25 48
// 37 26 4 3 5 12 8 15 35 17 11 24 10 27 20 28 7 1 38 21 6 13 16 14 2 33 19 41 42 40 45 34 36 9 30 18 50 43 47 32 44 46 22 31 49 29 23 39 25 48
// 37 26 4 3 5 12 8 15 26 17 11 12 10 27 20 28 7 1 38 21 6 13 16 14 2 33 19 42 50 40 45 34 36 9 30 18 35 43 47 32 44 46 22 31 49 29 23 39 25 24

Compilation message

swap.cpp: In function 'int main(int, const char**)':
swap.cpp:54: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 time Memory Grader output
1 Correct 3 ms 1916 KB Output is correct
2 Correct 3 ms 1916 KB Output is correct
3 Incorrect 3 ms 2076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1916 KB Output is correct
2 Correct 3 ms 1916 KB Output is correct
3 Incorrect 3 ms 2076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1916 KB Output is correct
2 Correct 3 ms 1916 KB Output is correct
3 Incorrect 3 ms 2076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1916 KB Output is correct
2 Correct 3 ms 1916 KB Output is correct
3 Incorrect 3 ms 2076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1916 KB Output is correct
2 Correct 3 ms 1916 KB Output is correct
3 Incorrect 3 ms 2076 KB Output isn't correct
4 Halted 0 ms 0 KB -