#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<piii> viii;
const int MAXN = 1000100;
int n;
ll d[1100][1100];
int par[MAXN], sz[MAXN];
int find (int a) {return par[a] = par[a]==a ? a : find(par[a]);}
bool same (int a, int b) {return find(a) == find(b);}
void unite (int a, int b) {
a = find(a), b = find (b);
if (a == b) return;
par[b] = a;
sz[a] += sz[b];
}
vector<pair<ll, pii>> edges;
long long minimum_walk(std::vector<int> p, int s) {
n = (int)p.size();
ll ret = 0ll;
FOR(i,0, n-1) ret += abs(i - p[i]);
FOR(i, 0, n-1) par[i] = i, sz[i] = 1;
FOR(i, 0, n-1) unite (i, p[i]);
FOR(i, 0, n-1) FOR(j, 0, n-1) d[i][j] = n + 5;
FOR(i, 0, n-1) FOR(j, i+1, n-1) {
int pi = find(i), pj = find(j);
d[pi][pj] = min(d[pi][pj], (ll)abs(j-i));
//d[pj][pi] = min(d[pj][pi], abs(j-i));
}
FOR(i, 0, n-1) FOR(j, i+1, n-1) {
if (find(i) != i || find(j) != j) continue;
if (!same(i,s) && !same(j,s) && (sz[i] == 1 || sz[j] == 1)) continue;
if (same(i,s) && sz[j] == 1) continue;
if (same(j,s) && sz[i] == 1) continue;
//cout << " edge: " << i << " - " << j << " w=" << d[i][j] << endl;
edges.pb({d[i][j], {i,j}});
}
sort(edges.begin(), edges.end());
ll sum = 0ll;
FOR(i, 0, n-1) par[i] = i;
for (auto e : edges) {
ll w = e.f;
int u = e.s.f, v = e.s.s;
if (same(u,v)) continue;
unite(u,v);
sum += w;
}
ret += 2*sum;
return ret;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Incorrect |
0 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Incorrect |
0 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Incorrect |
0 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
9596 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4108' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Incorrect |
0 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |