Submission #563467

#TimeUsernameProblemLanguageResultExecution timeMemory
563467ngpin04Swap (BOI16_swap)C++14
100 / 100
48 ms4716 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end() 
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());

int rand(int l, int r) {
	return l + rd() % (r - l + 1);
}
const int N = 4e5 + 5; 
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);

int sw[N];
int a[N];
int n;

int getmin(int i) {
	// cerr << "getmin " << i << " " << sw[i] << " " << sw[i ^ 1] << "\n";
	if (sw[i] == 0) 
		return a[i];

	if (i & 1) {
		if (sw[i - 1] == 1) 
			return a[i - 1];
		if (sw[i - 1] == 0)
			return getmin(i >> 1);
		return min(a[i - 1], getmin(i >> 1));
	} 

	if (sw[i] == 1) 
		return getmin(i >> 1);
	
	assert(sw[i] == -1);
	return min(a[i], getmin(i >> 1));
}	

void fix(int i, int val) {
	// cerr << "fix " << i << " " << val << "\n";
	if (sw[i] == 0) 
		return;

	if (i & 1) {
		if (sw[i - 1] == -1) {
			if (a[i - 1] == val) 
				sw[i - 1] = 1;
			else
				sw[i - 1] = 0;
		}

		if (sw[i - 1] == 0)
			fix(i >> 1, val);
		return;
	}

	if (sw[i] == 1) {
		fix(i >> 1, val);
		return;
	}

	assert(sw[i] == -1);
	if (a[i] == val) {
		sw[i] = 0;
		return;
	}
	
	sw[i] = 1;
	fix(i >> 1, val);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	#ifdef ONLINE_JUDGE
	// freopen(TASK".inp","r",stdin);
	// freopen(TASK".out","w",stdout);
	#endif
	cin >> n;
	for (int i = 1; i <= n; i++) 
		cin >> a[i];
	for (int i = n + 1; i <= 2 * n + 1; i++)
		a[i] = oo;

	for (int i = 1; i <= n; i++) {
		int l = i << 1;
		int r = i << 1 | 1;
		int mid = getmin(i);
		int res = min({a[l], mid, a[r]});

		if (mid == res) {
			fix(i, res);
			sw[l] = sw[r] = 0;
		} else if (a[l] == res) {
			sw[l] = 1;
			sw[r] = 0;
		} else {
			sw[l] = -1;
			sw[r] = 1;
		}
		// cout << "ans = " << res << "\n";
		cout << res << " ";
	}
	return 0;
}
#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...