#include <bits/stdc++.h>
#include "books.h"
#ifdef ONPC
#include "t_debug.cpp"
#else
#define debug(...) 42
#endif
#define sz(a) ((int)(a).size())
using namespace std;
using ll = long long;
const int INF = 1e9;
const ll INFL = 1e18;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p) { return is >> p.first >> p.second; }
const int N = 2e5, LOGN = 17, MOD = 1e9+7;
struct DSU {
int n;
vector<int> parent;
vector<int> size;
DSU(int n) : n(n), parent(n), size(n, 1) {
iota(parent.begin(), parent.end(), 0);
}
int get(int i) {
return parent[i] == i ? i : parent[i] = get(parent[i]);
}
void merge(int v, int u) {
v = get(v);
u = get(u);
if (u == v) return;
if (size[v] < size[u]) swap(v, u);
parent[v] = u;
size[v] += size[u];
}
};
long long minimum_walk(std::vector<int> p, int s) {
int n = sz(p);
vector<vector<int>> cycles;
vector<pair<int,int>> range;
vector<int> done(n);
int distance = 0;
int mnstart = -1;
for (int i = 0; i < n; i++) {
distance += abs(p[i] - i);
if (done[i]) continue;
if (p[i] == i) continue;
done[i] = true;
if (mnstart==-1) mnstart =i;
cycles.push_back({i});
for (int cur = p[i]; !done[cur]; cur = p[cur]) {
cycles.back().push_back(cur);
done[cur] = true;
}
}
int k = sz(cycles);
if (!k) return 0;
for (int i = 0; i < k; i++) {
int l = INF, r = 0;
for (int i : cycles[i]) {
l = min(l, i);
r = max(r, i);
}
range.emplace_back(l, r);
}
DSU groups(n);
for (int i = 0; i < k; i++) {
for (int j = range[i].first;j < range[i].second; j++) {
groups.merge(j, j+1);
}
}
int disjoint = 0;
bool prev = false;
for (int i = mnstart; i < n-1; i++) {
if (!prev && groups.get(i) != groups.get(i+1)) {
disjoint++;
prev = true;
} else {
prev = false;
}
}
int waste = disjoint * 2;
debug(cycles, range);
debug(waste, disjoint);
return distance + (mnstart == -1 ? 0 : mnstart * 2) + waste;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:6:24: warning: statement has no effect [-Wunused-value]
6 | #define debug(...) 42
| ^~
books.cpp:91:5: note: in expansion of macro 'debug'
91 | debug(cycles, range);
| ^~~~~
books.cpp:6:24: warning: statement has no effect [-Wunused-value]
6 | #define debug(...) 42
| ^~
books.cpp:92:5: note: in expansion of macro 'debug'
92 | debug(waste, disjoint);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
308 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |