#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
int N, A[MAXN+10];
vector<int> V[MAXN+10];
bool vis[MAXN+10][50];
vector<pii> dp[MAXN+10][50];
vector<pii> &solve(int now, int pp)
{
int qq=lower_bound(V[now].begin(), V[now].end(), pp)-V[now].begin();
if(vis[now][qq]) return dp[now][qq];
vis[now][qq]=1;
vector<pii> &ret=dp[now][qq];
if(now*2>N)
{
ret.push_back({now, A[pp]});
return ret;
}
if(now*2+1>N)
{
if(A[pp]>A[now*2])
{
ret.push_back({now, A[now*2]});
ret.push_back({now*2, A[pp]});
}
else
{
ret.push_back({now, A[pp]});
ret.push_back({now*2, A[now*2]});
}
return ret;
}
if(A[pp]<A[now*2] && A[pp]<A[now*2+1])
{
vector<pii> &p=solve(now*2, now*2), &q=solve(now*2+1, now*2+1);
ret.resize(p.size()+q.size()+1);
ret[0]={now, A[pp]};
merge(p.begin(), p.end(), q.begin(), q.end(), ret.begin()+1);
return ret;
}
if(A[now*2]<A[pp] && A[now*2]<A[now*2+1])
{
vector<pii> &p=solve(now*2, pp), &q=solve(now*2+1, now*2+1);
ret.resize(p.size()+q.size()+1);
ret[0]={now, A[now*2]};
merge(p.begin(), p.end(), q.begin(), q.end(), ret.begin()+1);
return ret;
}
if(A[now*2+1]<A[pp] && A[now*2+1]<A[now*2])
{
vector<pii> ret1, ret2;
vector<pii> &p1=solve(now*2, pp), &q1=solve(now*2+1, now*2);
ret1.resize(p1.size()+q1.size()+1);
ret1[0]={now, A[now*2+1]};
merge(p1.begin(), p1.end(), q1.begin(), q1.end(), ret1.begin()+1);
vector<pii> &p2=solve(now*2, now*2), &q2=solve(now*2+1, pp);
ret2.resize(p2.size()+q2.size()+1);
ret2[0]={now, A[now*2+1]};
merge(p2.begin(), p2.end(), q2.begin(), q2.end(), ret2.begin()+1);
ret=min(ret1, ret2);
return ret;
}
}
int main()
{
scanf("%d", &N);
for(int i=1; i<=N; i++) scanf("%d", &A[i]);
for(int i=1; i<=N; i++)
{
int now=i;
while(now!=1)
{
V[i].push_back(now);
if(now%2) V[i].push_back(now-1);
now/=2;
}
V[i].push_back(1);
reverse(V[i].begin(), V[i].end());
}
vector<pii> ans=solve(1, 1);
for(auto it : ans) printf("%d ", it.second);
}
Compilation message
swap.cpp: In function 'std::vector<std::pair<int, int> >& solve(int, int)':
swap.cpp:74:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
swap.cpp: In function 'int main()':
swap.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
swap.cpp:79:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1; i<=N; i++) scanf("%d", &A[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
239804 KB |
Output is correct |
2 |
Correct |
158 ms |
239864 KB |
Output is correct |
3 |
Correct |
169 ms |
239864 KB |
Output is correct |
4 |
Correct |
146 ms |
239864 KB |
Output is correct |
5 |
Correct |
148 ms |
239864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
239804 KB |
Output is correct |
2 |
Correct |
158 ms |
239864 KB |
Output is correct |
3 |
Correct |
169 ms |
239864 KB |
Output is correct |
4 |
Correct |
146 ms |
239864 KB |
Output is correct |
5 |
Correct |
148 ms |
239864 KB |
Output is correct |
6 |
Correct |
163 ms |
240016 KB |
Output is correct |
7 |
Correct |
159 ms |
239864 KB |
Output is correct |
8 |
Correct |
162 ms |
239864 KB |
Output is correct |
9 |
Correct |
152 ms |
240108 KB |
Output is correct |
10 |
Correct |
146 ms |
239864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
239804 KB |
Output is correct |
2 |
Correct |
158 ms |
239864 KB |
Output is correct |
3 |
Correct |
169 ms |
239864 KB |
Output is correct |
4 |
Correct |
146 ms |
239864 KB |
Output is correct |
5 |
Correct |
148 ms |
239864 KB |
Output is correct |
6 |
Correct |
163 ms |
240016 KB |
Output is correct |
7 |
Correct |
159 ms |
239864 KB |
Output is correct |
8 |
Correct |
162 ms |
239864 KB |
Output is correct |
9 |
Correct |
152 ms |
240108 KB |
Output is correct |
10 |
Correct |
146 ms |
239864 KB |
Output is correct |
11 |
Correct |
145 ms |
240120 KB |
Output is correct |
12 |
Correct |
152 ms |
240340 KB |
Output is correct |
13 |
Correct |
146 ms |
240120 KB |
Output is correct |
14 |
Correct |
148 ms |
240432 KB |
Output is correct |
15 |
Correct |
160 ms |
240508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
239804 KB |
Output is correct |
2 |
Correct |
158 ms |
239864 KB |
Output is correct |
3 |
Correct |
169 ms |
239864 KB |
Output is correct |
4 |
Correct |
146 ms |
239864 KB |
Output is correct |
5 |
Correct |
148 ms |
239864 KB |
Output is correct |
6 |
Correct |
163 ms |
240016 KB |
Output is correct |
7 |
Correct |
159 ms |
239864 KB |
Output is correct |
8 |
Correct |
162 ms |
239864 KB |
Output is correct |
9 |
Correct |
152 ms |
240108 KB |
Output is correct |
10 |
Correct |
146 ms |
239864 KB |
Output is correct |
11 |
Correct |
145 ms |
240120 KB |
Output is correct |
12 |
Correct |
152 ms |
240340 KB |
Output is correct |
13 |
Correct |
146 ms |
240120 KB |
Output is correct |
14 |
Correct |
148 ms |
240432 KB |
Output is correct |
15 |
Correct |
160 ms |
240508 KB |
Output is correct |
16 |
Runtime error |
196 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
239804 KB |
Output is correct |
2 |
Correct |
158 ms |
239864 KB |
Output is correct |
3 |
Correct |
169 ms |
239864 KB |
Output is correct |
4 |
Correct |
146 ms |
239864 KB |
Output is correct |
5 |
Correct |
148 ms |
239864 KB |
Output is correct |
6 |
Correct |
163 ms |
240016 KB |
Output is correct |
7 |
Correct |
159 ms |
239864 KB |
Output is correct |
8 |
Correct |
162 ms |
239864 KB |
Output is correct |
9 |
Correct |
152 ms |
240108 KB |
Output is correct |
10 |
Correct |
146 ms |
239864 KB |
Output is correct |
11 |
Correct |
145 ms |
240120 KB |
Output is correct |
12 |
Correct |
152 ms |
240340 KB |
Output is correct |
13 |
Correct |
146 ms |
240120 KB |
Output is correct |
14 |
Correct |
148 ms |
240432 KB |
Output is correct |
15 |
Correct |
160 ms |
240508 KB |
Output is correct |
16 |
Runtime error |
196 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
17 |
Halted |
0 ms |
0 KB |
- |