답안 #563467

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
563467 2022-05-17T09:45:55 Z ngpin04 Swap (BOI16_swap) C++14
100 / 100
48 ms 4716 KB
#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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 456 KB Output is correct
16 Correct 10 ms 1400 KB Output is correct
17 Correct 11 ms 1364 KB Output is correct
18 Correct 9 ms 1328 KB Output is correct
19 Correct 12 ms 1364 KB Output is correct
20 Correct 12 ms 1404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 456 KB Output is correct
16 Correct 10 ms 1400 KB Output is correct
17 Correct 11 ms 1364 KB Output is correct
18 Correct 9 ms 1328 KB Output is correct
19 Correct 12 ms 1364 KB Output is correct
20 Correct 12 ms 1404 KB Output is correct
21 Correct 38 ms 4616 KB Output is correct
22 Correct 40 ms 4640 KB Output is correct
23 Correct 44 ms 4628 KB Output is correct
24 Correct 48 ms 4716 KB Output is correct
25 Correct 46 ms 4616 KB Output is correct