#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 200001;
vector <bool> vv[nmax];
vector <vector <int> > g(nmax);
int a[nmax];
void bfs(int x){
queue <pii> q;
for(int i = 0; i < g[x].size(); i++){
q.push({g[x][i], i});
}
while(!q.empty()){
int v = q.front().f;
vv[x].pb(q.front().s);
q.pop();
for(int to : g[v]){
q.push({to, vv[x].back()});
}
}
}
map <pii, vector <int> > ans;
vector <int> opt(int v, int x){
if(!ans[{v, x}].empty()) return ans[{v, x}];
if(g[v].empty()){
return {x};
}
if(g[v].size() == 1){
int y = g[v][0];
if(a[y] < x){
return {a[y], x};
}
return {x, a[y]};
}
vector <int> A, B, C, D;
vector <int> all;
all.pb(x);
for(int to : g[v]){
all.pb(a[to]);
}
sort(all.begin(), all.end());
vector <int> nx, ny;
nx.pb(all[0]), ny.pb(all[0]);
A = opt(g[v][0], all[2]), B = opt(g[v][1], all[1]);
C = opt(g[v][0], all[1]); D = opt(g[v][1], all[2]);
int inda = 0, indb = 0;
for(auto to : vv[v]){
if(!to) nx.pb(A[inda++]);
else nx.pb(B[indb++]);
}
inda = indb = 0;
for(auto to : vv[v]){
if(!to) ny.pb(C[inda++]);
else ny.pb(D[indb++]);
}
return ans[{v, x}] = min(nx, ny);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
if(i * 2 <= n)g[i].pb(i * 2);
if(i *2 + 1<= n) g[i].pb(i * 2 + 1);
}
for(int i = 1; i <= n; i++)
bfs(i);
vector <int> op = opt(1, a[1]);
for(int to : op){
cout << to << ' ';
}
return 0;
}
Compilation message
swap.cpp: In function 'void bfs(int)':
swap.cpp:19:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(int i = 0; i < g[x].size(); i++){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
12864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
12864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
12864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
12864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
12864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |