// Om Namah Shivaya
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a, b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a, b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
https://usaco.guide/problems/baltic-oi-2016swap/solution
*/
const int MOD = 1e9 + 7;
const int N = 1e3 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
vector<int> merge(int root, vector<int> v1, vector<int> v2) {
vector<int> v3;
v3.pb(root);
int curr = 1;
int ptr = 0;
while (ptr < sz(v1) or ptr < sz(v2)) {
rep(j, curr) {
if (ptr < sz(v1)) {
v3.pb(v1[ptr]);
}
ptr++;
}
ptr -= curr;
rep(j, curr) {
if (ptr < sz(v2)) {
v3.pb(v2[ptr]);
}
ptr++;
}
curr *= 2;
}
return v3;
}
vector<int> a(N, inf1);
int n;
vector<int> dp[N][N];
vector<int> go(int i, int j) {
// lexi min bfs order of subtree i if a[i] = j
if (i > n) return {};
if (!dp[i][j].empty()) return dp[i][j];
int l = 2 * i;
int r = 2 * i + 1;
// no swaps
auto v1 = merge(j, go(l, a[l]), go(r, a[r]));
// swap root with left
auto v2 = merge(a[l], go(l, j), go(r, a[r]));
// swap root with right
auto v3 = merge(a[r], go(l, a[l]), go(r, j));
// swap root with left and then with right
auto v4 = merge(a[r], go(l, j), go(r, a[l]));
vector<int> res = min({v1, v2, v3, v4});
return dp[i][j] = res;
}
void solve(int test_case)
{
cin >> n;
rep1(i, n) cin >> a[i];
auto ans = go(1, a[1]);
trav(x, ans) cout << x << " ";
cout << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
Compilation message
swap.cpp: In function 'std::vector<int> merge(int, std::vector<int>, std::vector<int>)':
swap.cpp:70:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | while (ptr < sz(v1) or ptr < sz(v2)) {
| ^
swap.cpp:70:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | while (ptr < sz(v1) or ptr < sz(v2)) {
| ^
swap.cpp:72:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | if (ptr < sz(v1)) {
| ^
swap.cpp:81:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | if (ptr < sz(v2)) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24020 KB |
Output is correct |
2 |
Correct |
13 ms |
24044 KB |
Output is correct |
3 |
Correct |
14 ms |
24016 KB |
Output is correct |
4 |
Correct |
12 ms |
24020 KB |
Output is correct |
5 |
Correct |
13 ms |
24152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24020 KB |
Output is correct |
2 |
Correct |
13 ms |
24044 KB |
Output is correct |
3 |
Correct |
14 ms |
24016 KB |
Output is correct |
4 |
Correct |
12 ms |
24020 KB |
Output is correct |
5 |
Correct |
13 ms |
24152 KB |
Output is correct |
6 |
Correct |
13 ms |
24020 KB |
Output is correct |
7 |
Correct |
12 ms |
23964 KB |
Output is correct |
8 |
Correct |
13 ms |
24020 KB |
Output is correct |
9 |
Correct |
16 ms |
24020 KB |
Output is correct |
10 |
Correct |
14 ms |
24044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24020 KB |
Output is correct |
2 |
Correct |
13 ms |
24044 KB |
Output is correct |
3 |
Correct |
14 ms |
24016 KB |
Output is correct |
4 |
Correct |
12 ms |
24020 KB |
Output is correct |
5 |
Correct |
13 ms |
24152 KB |
Output is correct |
6 |
Correct |
13 ms |
24020 KB |
Output is correct |
7 |
Correct |
12 ms |
23964 KB |
Output is correct |
8 |
Correct |
13 ms |
24020 KB |
Output is correct |
9 |
Correct |
16 ms |
24020 KB |
Output is correct |
10 |
Correct |
14 ms |
24044 KB |
Output is correct |
11 |
Incorrect |
26 ms |
24552 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24020 KB |
Output is correct |
2 |
Correct |
13 ms |
24044 KB |
Output is correct |
3 |
Correct |
14 ms |
24016 KB |
Output is correct |
4 |
Correct |
12 ms |
24020 KB |
Output is correct |
5 |
Correct |
13 ms |
24152 KB |
Output is correct |
6 |
Correct |
13 ms |
24020 KB |
Output is correct |
7 |
Correct |
12 ms |
23964 KB |
Output is correct |
8 |
Correct |
13 ms |
24020 KB |
Output is correct |
9 |
Correct |
16 ms |
24020 KB |
Output is correct |
10 |
Correct |
14 ms |
24044 KB |
Output is correct |
11 |
Incorrect |
26 ms |
24552 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24020 KB |
Output is correct |
2 |
Correct |
13 ms |
24044 KB |
Output is correct |
3 |
Correct |
14 ms |
24016 KB |
Output is correct |
4 |
Correct |
12 ms |
24020 KB |
Output is correct |
5 |
Correct |
13 ms |
24152 KB |
Output is correct |
6 |
Correct |
13 ms |
24020 KB |
Output is correct |
7 |
Correct |
12 ms |
23964 KB |
Output is correct |
8 |
Correct |
13 ms |
24020 KB |
Output is correct |
9 |
Correct |
16 ms |
24020 KB |
Output is correct |
10 |
Correct |
14 ms |
24044 KB |
Output is correct |
11 |
Incorrect |
26 ms |
24552 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |