이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define inside sl<=l&&r<=sr
#define outside r<sl||sr<l
#define inf 1000000009
using namespace std;
typedef long long ll;
int n;
vector<int> arr;
vector<ll> numara;
vector<pair<int, pair<int, int> > > edges;
int num = 0;
vector<pair<ll, ll> > minler;
vector<int> dsu;
int find(int node){
if (dsu[node] == node) return node;
int ans = find(dsu[node]);
return dsu[node] = ans;
}
void unionn(int node1, int node2){
node1 = find(node1);
node2 = find(node2);
if (node1 == node2) return;
dsu[node1] = node2;
}
ll minimum_walk(vector<int> p, int s){
arr = p;
n = p.size();
numara.resize(n);
ll ans = 0;
for (int i = 0; i < n; i++) numara[i] = -1;
for (int i = 0; i < n; i++){
if (numara[i] == -1 && p[i] != i){
int node = i;
int lmost = node;
int rmost = node;
while (numara[node] == -1){
numara[node] = num;
node = p[node];
lmost = min(lmost, node);
rmost = max(rmost, node);
}
num++;
ans += (2*(rmost-lmost));
}
}
int last = -1;
for (int i = 0; i < n; i++){
if (numara[i] != -1){
if (last != -1) edges.pb(mp(i-last, mp(numara[i], numara[last])));
last = i;
}
}
dsu.resize(num);
for (int i = 0; i < num; i++) dsu[i] = i;
sort(edges.begin(), edges.end());
for (int i = 0; i < edges.size(); i++){
if (find(edges[i].second.first) != find(edges[i].second.second)){
ans += edges[i].first;
unionn(edges[i].second.first, edges[i].second.second);
}
}
int mindist = inf;
for (int i = s; i < n; i++){
if (numara[i] != -1){
mindist = min(mindist, i-s);
break;
}
}
for (int i = s; i >= 0; i--){
if (numara[i] != -1){
mindist = min(mindist, s-i);
break;
}
}
ans += 2*mindist;
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:67:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < edges.size(); 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... |