#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);
set<int>one;
for(int i = 0; i < n; i++)
{
if(!val[i])
{
int col = ++nxt, cur = i, cnt = 0, chk = 0;
while(!val[cur])
{
if(cur == s)
chk = 1;
cnt++;
val[cur] = col;
ans += abs(p[cur] - cur);
cur = p[cur];
}
if(cnt == 1 && !chk)
one.insert(col);
}
}
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(one.count(aa.y.x) || one.count(aa.y.y))
continue;
if(unite(aa.y.x, aa.y.y))
ans += aa.x * 2;
}
return ans;
}
void clr()
{
edges.clear();
memset(val, 0, sizeof val);
}
/*int main()
{
ios_base::sync_with_stdio(false), cin.tie(0);
cout << minimum_walk({0, 3, 5, 2, 4, 1}, 0);
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
7544 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4104' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |