#include <bits/stdc++.h>
#include "books.h"
#pragma GCC optimize("Ofast")
#define x first
#define y second
#define ld long double
#define ll long long
using namespace std;
int n, s, x;
map <pair <int, int>, int> calc;
void f(int &l, int &r, vector <int> L, vector <int> R, vector <int> cyc) {
int st = min(l, min(L[cyc[l]], L[cyc[r]])), dr = max(r, max(R[cyc[l]], R[cyc[r]]));
while(st < l || r < dr) {
if(st < l) {
l--;
st = min(st, L[cyc[l]]);
dr = max(dr, R[cyc[l]]);
} else {
r++;
st = min(st, L[cyc[r]]);
dr = max(dr, R[cyc[r]]);
}
}
}
int dp(int l, int r, vector <int> L, vector <int> R, vector <int> cyc) {
f(l, r, L, R, cyc);
if(calc.find({l, r}) != calc.end())
return calc[{l, r}];
int ans = (int)1e9;
int st, dr;
if(l) {
st = l - 1; dr = r;
f(st, dr, L, R, cyc);
ans = min(ans, 2 + dp(st, dr, L, R, cyc));
}
if(r < cyc.size() - 1) {
st = l; dr = r + 1;
f(st, dr, L, R, cyc);
ans = min(ans, 2 + dp(st, dr, L, R, cyc));
}
calc[{l, r}] = ans;
return ans;
}
ll minimum_walk(vector <int> p, int s) {
ll ans = 2;
int n = p.size(), c = 0, st = s, dr = s;
vector <int> cyc(n + 5), l(n + 5), r(n + 5);
fill(cyc.begin(), cyc.end(), -1);
calc.clear();
for(int i = 0; i < n; i++) {
ans += abs(i - p[i]);
if(cyc[i] == -1) {
l[c] = r[c] = i;
int j = i;
while(cyc[j] == -1) {
cyc[j] = c;
r[c] = max(r[c], j);
j = p[j];
}
if(i != p[i]) {
st = min(st, l[c]);
dr = max(dr, r[c]);
}
c++;
}
}
calc[{st, dr}] = 0;
return ans + dp(st, dr, l, r, cyc);
}
/*int main() {
//ios_base :: sync_with_stdio(false);
//cin.tie(0); cout.tie(0);
cin >> n >> s;
vector <int> p;
for(int i = 1; i <= n; i++)
cin >> x, p.push_back(x);
cout << minimum_walk(p, s);
return 0;
}
*/
Compilation message
books.cpp: In function 'int dp(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
books.cpp:41:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if(r < cyc.size() - 1) {
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Incorrect |
0 ms |
364 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Incorrect |
0 ms |
364 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Incorrect |
0 ms |
364 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
376 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2746' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Incorrect |
0 ms |
364 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
4 |
Halted |
0 ms |
0 KB |
- |