이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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';
}
컴파일 시 표준 에러 (stderr) 메시지
swap.cpp: In function 'int Findup(int)':
swap.cpp:38:1: warning: control reaches end of non-void function [-Wreturn-type]
38 | }
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |