이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double llf;
int n, a[530000];
map<int, int> sol[200005];
int solve(int x, int r){
if(x > n) return -1;
if(sol[x].find(r) != sol[x].end()) return sol[x][r];
int minval = min({r, a[2*x], a[2*x+1]});
if(minval == r){
solve(2*x, a[2*x]);
solve(2*x+1, a[2*x+1]);
return sol[x][r] = x;
}
else if(minval == a[2*x]){
int ret = solve(2*x, r);
solve(2*x+1, a[2*x+1]);
return sol[x][r] = ret;
}
else{
int k1 = solve(2*x, r);
int k2 = solve(2*x+1, a[2*x]);
int k3 = solve(2*x, a[2*x]);
int k4 = solve(2*x+1, r);
if(min(k1, k2) < min(k3, k4)){
return sol[x][r] = k1;
}
if(min(k1, k2) > min(k3, k4)){
return sol[x][r] = k4;
}
if(r < a[2*x]){
if(k1 < k4){
return sol[x][r] = k1;
}
else{
return sol[x][r] = k4;
}
}
else{
if(k2 < k3){
return sol[x][r] = k1;
}
else{
return sol[x][r] = k4;
}
}
}
}
void fmerge(vector<int> &ret, vector<int> &v1, vector<int> &v2){
int pnt = 0;
for(int i=1; pnt < max(v1.size(), v2.size()); i<<=1){
for(int j=pnt; j<pnt + i && j < v1.size(); j++){
ret.push_back(v1[j]);
}
for(int j=pnt; j<pnt + i && j < v2.size(); j++){
ret.push_back(v2[j]);
}
pnt+=i;
}
}
vector<int> track(int x, int r){
if(x > n) return vector<int>();
int minval = min({r, a[2*x], a[2*x+1]});
if(minval == r){
auto a1 = vector<int>{r};
auto a2 = track(2*x, a[2*x]);
auto a3 = track(2*x+1, a[2*x+1]);
fmerge(a1, a2, a3);
return a1;
}
else if(minval == a[2*x]){
auto a1 = vector<int>{a[2*x]};
auto a2 = track(2*x, r);
auto a3 = track(2*x+1, a[2*x+1]);
fmerge(a1, a2, a3);
return a1;
}
else{
int k1 = solve(2*x, r);
int k4 = solve(2*x+1, r);
if(sol[x][r] == k1){
auto a1 = vector<int>{a[2*x+1]};
auto a2 = track(2*x, r);
auto a3 = track(2*x+1, a[2*x]);
fmerge(a1, a2, a3);
return a1;
}
else{
auto a1 = vector<int>{a[2*x+1]};
auto a2 = track(2*x, a[2*x]);
auto a3 = track(2*x+1, r);
fmerge(a1, a2, a3);
return a1;
}
}
}
int main(){
memset(a, 0x3f, sizeof(a));
scanf("%d",&n);
for(int i=1; i<=n; i++){
scanf("%d",&a[i]);
}
solve(1, a[1]);
auto w = track(1, a[1]);
for(auto &i : w){
printf("%d ",i);
}
}
컴파일 시 표준 에러 (stderr) 메시지
swap.cpp: In function 'void fmerge(std::vector<int>&, std::vector<int>&, std::vector<int>&)':
swap.cpp:56:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1; pnt < max(v1.size(), v2.size()); i<<=1){
^
swap.cpp:57:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=pnt; j<pnt + i && j < v1.size(); j++){
^
swap.cpp:60:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=pnt; j<pnt + i && j < v2.size(); j++){
^
swap.cpp: In function 'std::vector<int> track(int, int)':
swap.cpp:86:7: warning: unused variable 'k4' [-Wunused-variable]
int k4 = solve(2*x+1, r);
^
swap.cpp: In function 'int main()':
swap.cpp:107:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
^
swap.cpp:109:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i]);
^
# | 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... |