#include "books.h"
#include <bits/stdc++.h>
#define lli long long
#define ad push_back
#define fr first
#define sc second
#define MP make_pair
using namespace std;
const int N = 1e6 + 30;
int i1, n;
int col[N], a[N], p[N];
int gp(int x)
{
if(p[x] == x) return x;
return p[x] = gp(p[x]);
}
vector<pair<lli, pair<int, int> > > fp;
lli ans;
void dfs(int v)
{
col[v] = i1;
if(!col[a[v]]) dfs(a[v]);
}
long long minimum_walk(vector<int> p, int s)
{
n = p.size();
for(int i = 0; i < n; i++) a[i] = p[i], ans += abs(a[i] - i), ::p[i] = i;
//cout << ans << endl;
int nx = 0;
for(int i = 0; i < n; i++)
{
if(a[i] == i) continue;
if(!col[i]) i1++, dfs(i);
if(col[i] != col[nx]) fp.ad(MP(2*(i - nx), MP(col[nx], col[i])));
nx = i;
}
sort(fp.begin(), fp.end());
//cout << gp(0) << " " << gp(1) << endl;
for(auto p : fp)
{
//cout << p.fr << " " << p.sc.fr << " " << p.sc.sc << endl;
if(gp(p.sc.fr) == gp(p.sc.sc)) continue;
ans += p.fr;
::p[gp(p.sc.fr)] = gp(p.sc.sc);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4074' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |