이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "books.h"
#include <bits/stdc++.h>
using namespace std;
bool in[1000005];
vector<pair<int, int> > cyc, tmp;
int mn, mx;
long long res;
void dfs(int i, vector<int>& p){
if(in[i]) return;
in[i] = 1;
mx = max(mx, i);
mn = min(mn, i);
dfs(p[i], p);
}
long long minimum_walk(std::vector<int> p, int s) {
for(int i = 0; i < p.size(); i++){
res += abs(p[i] - i);
if(in[i]) continue;
mn = i;
mx = i;
dfs(i, p);
if(mn != mx)tmp.push_back({mn , mx});
else in[i] = 0;
}
if(tmp.empty()) return 0;
int l = s, r = s;
while(l && in[l] == 0)l--;
while(r != p.size() && in[r] == 0) r++;
if(l == -1) res += (r - s) * 2;
else if(r == p.size()) res += (s - l) * 2;
else {
res += min(s - l, r - s) * 2;
}
sort(tmp.begin(), tmp.end());
mx = -1;
for(auto& x : tmp){
if(mx < x.first){
if(!cyc.empty()){
res += 2 * (x.first - mx);
}
cyc.push_back(x);
}
mx = max(mx, x.second);
}
return res;
}
컴파일 시 표준 에러 (stderr) 메시지
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:18:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < p.size(); i++){
~~^~~~~~~~~~
books.cpp:30:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(r != p.size() && in[r] == 0) r++;
~~^~~~~~~~~~~
books.cpp:32:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
else if(r == p.size()) res += (s - l) * 2;
~~^~~~~~~~~~~
# | 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... |