#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "books.h"
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
long long minimum_walk(vector<int> p, int s) {
int n = p.size();
vector <int> vis(n, 0);
vector <pair <int, int>> S;
long long tot = 0;
for (int u = 0; u < n; ++u) {
tot += abs(u - p[u]);
if (vis[u]) continue;
int v = u;
int mini = 1e9, maxi = - 1e9;
do {
mini = min(mini, v);
maxi = max(maxi, v);
v = p[v];
vis[v] = 1;
} while (u != v);
S.push_back({mini, -1});
S.push_back({maxi, 1});
}
sort(S.begin(), S.end());
int cur = 0;
int E = 0;
for (auto [x, sign]: S) {
cur -= sign;
if (cur == 0) ++E;
}
// cout << tot << '\n';
--E;
return tot + E * 2;
}
//
//int main() {
// int n, s;
// assert(2 == scanf("%d %d", &n, &s));
//
// vector<int> p((unsigned) n);
// for(int i = 0; i < n; i++)
// assert(1 == scanf("%d", &p[(unsigned) i]));
//
// long long res = minimum_walk(p, s);
// printf("%lld\n", res);
//
// return 0;
//}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:36:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
36 | for (auto [x, sign]: S) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 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 |
340 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 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 |
- |