#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 5;
const int INF = 1e9;
int n;
int A[MAXN], state[MAXN];
int Findup(int id)
{
int ans = INF;
while(id > 0)
{
// cout << id << "o" << endl;
// cout << A[id] << endl;
if(state[id] != 1)
{
ans = min(ans, A[id]);
}
if(state[id] == 0)
{
return ans;
}
if(id & 1)
{
if(state[id - 1] == 1)
{
return min(ans, A[id - 1]);
}
if(state[id - 1] == -1)
{
ans = min(ans, A[id - 1]);
}
}
id >>= 1;
}
}
void Move(int id, int val)
{
while(id)
{
// cout << id << "H" << endl;
if(A[id] == val)
{
state[id] = 0;
return;
}
if(id & 1)
{
if(A[id - 1] == val)
{
state[id - 1] = 1;
state[id] = 1;
break;
}
else
{
state[id - 1] = 0;
}
}
state[id] = 1;
id >>= 1;
}
}
int main()
{
// freopen("Input.txt", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.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] = INF;
}
for(int i = 1; i <= 2 * n; i++)
{
state[i] = -1;
}
state[1] = 0;
for(int i = 1; i <= n; i++)
{
int v = Findup(i);
int tmp = min({v, A[2 * i], A[2 * i + 1]});
if(tmp == v)
{
Move(i, v);
state[2 * i] = 0;
state[2 * i + 1] = 0;
}
else if(tmp == A[2 * i])
{
state[2 * i] = 1;
state[2 * i + 1] = 0;
}
else if(tmp == A[2 * i + 1])
{
state[2 * i + 1] = 1;
}
// for(int j = 2; j <= n; j++)
// {
// cout << state[j] << ' ';
// }
// cout << '\n';
cout << tmp << ' ';
}
cout << '\n';
}
Compilation message
swap.cpp: In function 'int Findup(int)':
swap.cpp:38:1: warning: control reaches end of non-void function [-Wreturn-type]
38 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
9 ms |
1612 KB |
Output is correct |
17 |
Correct |
9 ms |
1576 KB |
Output is correct |
18 |
Correct |
10 ms |
1628 KB |
Output is correct |
19 |
Correct |
12 ms |
1612 KB |
Output is correct |
20 |
Correct |
12 ms |
1612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
9 ms |
1612 KB |
Output is correct |
17 |
Correct |
9 ms |
1576 KB |
Output is correct |
18 |
Correct |
10 ms |
1628 KB |
Output is correct |
19 |
Correct |
12 ms |
1612 KB |
Output is correct |
20 |
Correct |
12 ms |
1612 KB |
Output is correct |
21 |
Correct |
43 ms |
5932 KB |
Output is correct |
22 |
Correct |
41 ms |
5916 KB |
Output is correct |
23 |
Correct |
42 ms |
5860 KB |
Output is correct |
24 |
Correct |
47 ms |
5956 KB |
Output is correct |
25 |
Correct |
48 ms |
5912 KB |
Output is correct |