이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "books.h"
using namespace std;
typedef long long ll;
vector<ll> lnk;
ll find(ll a) {
if (lnk[a] != a) lnk[a] = find(lnk[a]);
return lnk[a];
}
void unite(ll a, ll b) {
a = find(a); b = find(b);
lnk[a] = b;
}
ll minimum_walk(vector<int> p, int s) {
ll n = p.size();
ll res = 0;
for (int i = 0; i < n; i++) {
if (p[i] != i && res == 0) res = 2*i;
res += abs(p[i] - i);
}
vector<bool> vst(n);
vector<vector<ll>> cyc;
for (int i = 0; i < n; i++) {
if (vst[i] || p[i] == i) continue;
cyc.push_back(vector<ll>());
ll cur = i;
do {
cyc.back().push_back(cur);
cur = p[cur];
vst[cur] = true;
} while (cur != i);
}
priority_queue<pair<ll, pair<ll, ll>>> q;
for (int a = 0; a < cyc.size(); a++) {
ll mnA = n, mxA = 0;
for (auto &e : cyc[a]) { mnA = min(mnA, e); mxA = max(mxA, e); }
for (int b = a+1; b < cyc.size(); b++) {
ll mnB = n, mxB = 0;
for (auto &e : cyc[b]) { mnB = min(mnB, e); mxB = max(mxB, e); }
ll mn = n;
for (int i = 0; i < cyc[a].size(); i++) {
for (int j = 0; j < cyc[b].size(); j++) {
mn = min(mn, abs(cyc[a][i] - cyc[b][j]));
if (cyc[a][i] > mnB && cyc[a][i] < mxB) mn = 0;
if (cyc[b][i] > mnA && cyc[b][i] < mxA) mn = 0;
}
}
q.push({-mn, {a, b}});
}
}
lnk = vector<ll>(cyc.size());
for (int i = 0; i < cyc.size(); i++) lnk[i] = i;
while (!q.empty()) {
ll cost = -q.top().first;
pair<ll, ll> p = q.top().second;
q.pop();
if (find(p.first) != find(p.second)) {
unite(p.first, p.second);
res += 2*cost;
}
}
return res;
}
컴파일 시 표준 에러 (stderr) 메시지
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int a = 0; a < cyc.size(); a++) {
| ~~^~~~~~~~~~~~
books.cpp:45:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int b = a+1; b < cyc.size(); b++) {
| ~~^~~~~~~~~~~~
books.cpp:49:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i = 0; i < cyc[a].size(); i++) {
| ~~^~~~~~~~~~~~~~~
books.cpp:50:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int j = 0; j < cyc[b].size(); j++) {
| ~~^~~~~~~~~~~~~~~
books.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 0; i < cyc.size(); i++) lnk[i] = 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... |