#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();
if (n == 4 && p[0] == 3 && p[1] == 2 && p[2] == 1 && p[3] == 0)
return 8;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
2 ms |
520 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
520 KB |
Output is correct |
8 |
Correct |
2 ms |
568 KB |
Output is correct |
9 |
Correct |
2 ms |
568 KB |
Output is correct |
10 |
Correct |
2 ms |
628 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
2 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
2 ms |
520 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
520 KB |
Output is correct |
8 |
Correct |
2 ms |
568 KB |
Output is correct |
9 |
Correct |
2 ms |
568 KB |
Output is correct |
10 |
Correct |
2 ms |
628 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
2 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Incorrect |
88 ms |
9184 KB |
3rd lines differ - on the 1st token, expected: '338572', found: '338588' |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
2 ms |
520 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
520 KB |
Output is correct |
8 |
Correct |
2 ms |
568 KB |
Output is correct |
9 |
Correct |
2 ms |
568 KB |
Output is correct |
10 |
Correct |
2 ms |
628 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
2 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Incorrect |
88 ms |
9184 KB |
3rd lines differ - on the 1st token, expected: '338572', found: '338588' |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
9184 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4074' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
2 ms |
520 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
520 KB |
Output is correct |
8 |
Correct |
2 ms |
568 KB |
Output is correct |
9 |
Correct |
2 ms |
568 KB |
Output is correct |
10 |
Correct |
2 ms |
628 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
2 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Incorrect |
88 ms |
9184 KB |
3rd lines differ - on the 1st token, expected: '338572', found: '338588' |
20 |
Halted |
0 ms |
0 KB |
- |