#include "books.h"
#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 1e6 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, s, val[maxn], nxt;
vector<vector<int> > edge;
vector<int>p;
vector<pair<int, pii> > edges;
ll ans;
int par[maxn];
int root(int v)
{
return v == par[v] ? v : par[v] = root(par[v]);
}
bool unite(int a, int b)
{
a = root(a), b = root(b);
if(a == b)
return false;
par[b] = a;
return true;
}
ll minimum_walk(vector<int>perm, int S)
{
s = S;
p = perm;
n = sz(p);
for(int i = 0; i < n; i++)
{
if(!val[i])
{
int col = ++nxt, cur = i;
while(!val[cur])
{
val[cur] = col;
ans += abs(p[cur] - cur);
cur = p[cur];
}
}
}
edge = vector<vector<int> > (n, vector<int>(n, mod));
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if(val[i] != val[j])
edge[val[i]][val[j]] = min(edge[val[i]][val[j]], j - i);
}
}
for(int i = 1; i <= nxt; i++)
par[i] = i;
for(int i = 1; i <= nxt; i++)
for(int j = i + 1; j <= nxt; j++)
edges.pb({edge[i][j], {i, j}});
sort(all(edges));
for(auto aa : edges)
{
if(unite(aa.y.x, aa.y.y))
ans += aa.x * 2;
}
return ans;
}
/*int main()
{
ios_base::sync_with_stdio(false), cin.tie(0);
cout << minimum_walk({0, 2, 3, 1}, 0);
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
40 ms |
7540 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4186' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |