#include <bits/stdc++.h>
#include "books.h"
#define ll long long
using namespace std;
//#define DEBUGGING
#ifdef DEBUGGING
#include "../debug.h"
#else
#define debug(x...) void(42)
#endif
struct Node
{
int head, tail, size, nxt;
Node(int i) : head(i), tail(i), size(1), nxt(-1) {}
Node() : Node(-1) {}
};
constexpr static int MXN = 1e6;
Node node[MXN];
bitset<MXN> visited;
void merge(int a, int b);
ll minimum_walk(vector<int> p, int s)
{
ll sum = 0;
int n = p.size();
debug("A");
for (int i = 0; i < n; i++)
sum += abs(p[i] - i);
debug("B");
for (int i = 0; i < n; i++)
node[i] = Node(i);
debug("c");
for (int i = 0; i < n; i++)
if (node[i].head != node[p[i]].head)
merge(node[i].head, node[p[i]].head);
for (int i = 0; i < n; i++)
debug(i, node[i].head);
debug(sum);
debug("D");
int last = s;
for (int i = s; i >= 0; i--)
{
if (!visited[node[i].head])
{
sum += (last - i) * 2;
visited[node[i].head] = true;
i = last;
}
}
debug("E");
last = s;
for (int i = s; i < n; i++)
{
if (!visited[node[i].head])
{
sum += (i - last) * 2;
last = i;
visited[node[i].head] = true;
}
}
return sum;
}
void merge(int a, int b)
{
if (node[a].size < node[b].size) swap(a, b);
node[a].size += node[b].size;
node[node[a].tail].nxt = b;
node[a].tail = node[b].tail;
for (;b!=-1;b=node[b].nxt)
node[b].head = a;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15828 KB |
Output is correct |
2 |
Incorrect |
7 ms |
15956 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15828 KB |
Output is correct |
2 |
Incorrect |
7 ms |
15956 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15828 KB |
Output is correct |
2 |
Incorrect |
7 ms |
15956 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
15956 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '131942' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15828 KB |
Output is correct |
2 |
Incorrect |
7 ms |
15956 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |