#include <bits/stdc++.h>
#include "books.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using plpii = pair<ll, pii>;
const int N = 1e6+10;
const ll INF = 1e9;
int par[N];
int root(int u) {
if (par[u] == u) return u;
return par[u] = root(par[u]);
}
bool merge(int u, int v) {
u = root(u), v = root(v);
if (u == v) return false;
par[u] = v;
return true;
}
ll minimum_walk(vector<int> p, int s)
{
int n = p.size();
iota(par, par+n, 0);
ll dist = 0;
for (int i = 0; i < n; ++i) {
if (p[i] == -1) continue;
if (p[i] == i) continue;
int u = i;
while (p[u] != -1) {
int v = p[u];
dist += abs(u-v);
p[u] = -1;
merge(u, v);
u = v;
}
}
if (p[s] == s) p[s] = -1;
vector<plpii> E;
for (int i = 0; i < n; ++i) if (p[i] == -1) {
for (int j = i+1; j < n; ++j) if (p[j] = -1) {
E.emplace_back(j-i, pii(i, j));
}
}
sort(E.begin(), E.end());
for (auto e : E) {
int w = e.first, u, v;
tie(u, v) = e.second;
if (merge(u, v)) dist += 2*w;
}
return dist;
}
Compilation message
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:46:48: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
for (int j = i+1; j < n; ++j) if (p[j] = -1) {
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Incorrect |
2 ms |
484 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Incorrect |
2 ms |
484 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Incorrect |
2 ms |
484 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
179 ms |
8836 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4186' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Incorrect |
2 ms |
484 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |