#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;
bitset<MXN> visited_component;
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);
int counter = 0;
for (int i = 0; i < n; i++)
if (node[i].head == i && node[i].size > 1)
counter++;
debug(sum);
debug("D");
priority_queue<array<int, 2>> pq;
for (int i = node[s].head; i != -1; i = node[i].nxt)
{
pq.push({0, i});
}
while (pq.size())
{
auto [dist, i] = pq.top();
pq.pop();
if (visited[i])
continue;
visited[i] = true;
dist *= -1;
if (!visited_component[node[i].head] && node[node[i].head].size > 1)
{
sum += 2 * dist;
dist = 0;
for (int j = node[i].head; j != -1; j = node[j].nxt)
pq.push({0, j});
}
if (i > 0)
pq.push({-(dist+1), i-1});
if (i < n-1)
pq.push({-(dist+1), i+1});
}
int lle = s, lri = s;
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;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:76:6: warning: unused variable 'lle' [-Wunused-variable]
76 | int lle = s, lri = s;
| ^~~
books.cpp:76:15: warning: unused variable 'lri' [-Wunused-variable]
76 | int lle = s, lri = s;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15904 KB |
Output is correct |
2 |
Correct |
7 ms |
15924 KB |
Output is correct |
3 |
Correct |
8 ms |
15964 KB |
Output is correct |
4 |
Correct |
7 ms |
15896 KB |
Output is correct |
5 |
Correct |
7 ms |
15964 KB |
Output is correct |
6 |
Incorrect |
7 ms |
15916 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 |
7 ms |
15904 KB |
Output is correct |
2 |
Correct |
7 ms |
15924 KB |
Output is correct |
3 |
Correct |
8 ms |
15964 KB |
Output is correct |
4 |
Correct |
7 ms |
15896 KB |
Output is correct |
5 |
Correct |
7 ms |
15964 KB |
Output is correct |
6 |
Incorrect |
7 ms |
15916 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 |
7 ms |
15904 KB |
Output is correct |
2 |
Correct |
7 ms |
15924 KB |
Output is correct |
3 |
Correct |
8 ms |
15964 KB |
Output is correct |
4 |
Correct |
7 ms |
15896 KB |
Output is correct |
5 |
Correct |
7 ms |
15964 KB |
Output is correct |
6 |
Incorrect |
7 ms |
15916 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 |
7 ms |
15956 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4074' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15904 KB |
Output is correct |
2 |
Correct |
7 ms |
15924 KB |
Output is correct |
3 |
Correct |
8 ms |
15964 KB |
Output is correct |
4 |
Correct |
7 ms |
15896 KB |
Output is correct |
5 |
Correct |
7 ms |
15964 KB |
Output is correct |
6 |
Incorrect |
7 ms |
15916 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |