Submission #563346

# Submission time Handle Problem Language Result Execution time Memory
563346 2022-05-17T02:15:17 Z ngpin04 Swap (BOI16_swap) C++14
0 / 100
2 ms 4180 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 dp[1005][1005];
int a[N];
int n;

int solve(int v, int pos) {
	int &res = dp[v][pos];
	if (res != -1)
		return res;
	int mn = min({v, a[pos << 1], a[pos << 1 | 1]});
	if (v == mn)
		return res = pos;
	if (a[pos << 1] == mn)
		return res = solve(v, pos << 1);
	return res = min(solve(v, pos << 1), solve(v, pos << 1 | 1));
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	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;
		
	memset(dp, -1, sizeof(dp));
	for (int i = 1; i <= n; i++) {
		int mn = min({a[i], a[i << 1], a[i << 1 | 1]});
		if (a[i] == mn)
			continue;
		if (a[i << 1] == mn) {
			swap(a[i], a[i << 1]);
			continue;
		}
		
		swap(a[i], a[i << 1 | 1]);
		if (a[i << 1] > a[i << 1 | 1])
			swap(a[i << 1], a[i << 1 | 1]);
		
		if (solve(a[i << 1], i << 1) > solve(a[i << 1], i << 1 | 1))
			swap(a[i << 1], a[i << 1 | 1]);
	}
	
	for (int i = 1; i <= n; i++)
		cout << a[i] << " ";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4180 KB Output is correct
3 Correct 2 ms 4180 KB Output is correct
4 Incorrect 2 ms 4180 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4180 KB Output is correct
3 Correct 2 ms 4180 KB Output is correct
4 Incorrect 2 ms 4180 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4180 KB Output is correct
3 Correct 2 ms 4180 KB Output is correct
4 Incorrect 2 ms 4180 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4180 KB Output is correct
3 Correct 2 ms 4180 KB Output is correct
4 Incorrect 2 ms 4180 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4180 KB Output is correct
3 Correct 2 ms 4180 KB Output is correct
4 Incorrect 2 ms 4180 KB Output isn't correct
5 Halted 0 ms 0 KB -