#include "books.h"
#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 1e6 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, s, val[maxn], nxt, L[maxn], R[maxn], fr, to;
ll base, add;
vector<int>p;
map<pii, int>dp;
pii extend(int l, int r)
{
int cur_l = l, cur_r = r;
int i = l, j = r;
while(i >= cur_l || j <= cur_r)
{
if(i >= cur_l)
{
cur_l = min(cur_l, L[val[i]]);
cur_r = max(cur_r, R[val[i]]);
--i;
}
if(j <= cur_r)
{
cur_l = min(cur_l, L[val[j]]);
cur_r = max(cur_r, R[val[j]]);
j++;
}
}
return {cur_l, cur_r};
}
ll solve(int l, int r)
{
if(dp.count(pii(l, r)))
return dp[pii(l, r)];
pii aa = extend(l, r);
int cur_l = aa.x, cur_r = aa.y;
if(cur_l <= fr && cur_r >= to)
return 0;
if(cur_l <= fr)
return dp[pii(l, r)] = 2 + solve(cur_l, cur_r + 1);
if(cur_r >= to)
return dp[pii(l, r)] = 2 + solve(cur_l - 1, cur_r);
pii a = extend(cur_l, cur_r + 1);
pii b = extend(cur_l - 1, cur_r);
if(a.x <= b.x && a.y >= b.y)
return dp[pii(l, r)] = 2 + solve(cur_l, cur_r + 1);
else
return dp[pii(l, r)] = 2 + solve(cur_l - 1, cur_r);
}
ll minimum_walk(vector<int>perm, int S)
{
p = perm;
s = S;
n = sz(p);
for(int i = 1; i <= n; i++)
L[i] = mod, R[i] = -mod;
to = n - 1, fr = 0;
while(to >= 0 && p[to] == to)
--to;
while(fr < n && p[fr] == fr)
++fr;
if(fr > to)
return 0;
for(int i = 0; i < n; i++)
{
if(val[i] == 0)
{
int col = ++nxt, cur = i;
while(val[cur] == 0)
{
val[cur] = col;
L[col] = min(L[col], cur);
R[col] = max(R[col], cur);
base += abs(p[cur] - cur);
cur = p[cur];
}
}
}
return base + solve(s, s);
}
/*int main()
{
ios_base::sync_with_stdio(false), cin.tie(0);
vector<int>perm;
ifstream fin("in.txt");
int n, s;
fin >> n >> s;
perm.resize(n);
for(int i = 0; i < n; i++)
fin >> perm[i];
cout << minimum_walk(perm, s) << endl;
return 0;
}*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
6 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
6 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
6 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
322 ms |
24688 KB |
Output is correct |
31 |
Correct |
283 ms |
24696 KB |
Output is correct |
32 |
Correct |
152 ms |
20600 KB |
Output is correct |
33 |
Correct |
422 ms |
114772 KB |
Output is correct |
34 |
Correct |
466 ms |
114680 KB |
Output is correct |
35 |
Correct |
342 ms |
88952 KB |
Output is correct |
36 |
Correct |
221 ms |
43516 KB |
Output is correct |
37 |
Correct |
227 ms |
25592 KB |
Output is correct |
38 |
Correct |
170 ms |
24680 KB |
Output is correct |
39 |
Correct |
166 ms |
24672 KB |
Output is correct |
40 |
Correct |
193 ms |
24824 KB |
Output is correct |
41 |
Correct |
214 ms |
24568 KB |
Output is correct |
42 |
Correct |
238 ms |
24568 KB |
Output is correct |
43 |
Correct |
174 ms |
24824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Incorrect |
3 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '10478', found: '10490' |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
6 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
322 ms |
24688 KB |
Output is correct |
31 |
Correct |
283 ms |
24696 KB |
Output is correct |
32 |
Correct |
152 ms |
20600 KB |
Output is correct |
33 |
Correct |
422 ms |
114772 KB |
Output is correct |
34 |
Correct |
466 ms |
114680 KB |
Output is correct |
35 |
Correct |
342 ms |
88952 KB |
Output is correct |
36 |
Correct |
221 ms |
43516 KB |
Output is correct |
37 |
Correct |
227 ms |
25592 KB |
Output is correct |
38 |
Correct |
170 ms |
24680 KB |
Output is correct |
39 |
Correct |
166 ms |
24672 KB |
Output is correct |
40 |
Correct |
193 ms |
24824 KB |
Output is correct |
41 |
Correct |
214 ms |
24568 KB |
Output is correct |
42 |
Correct |
238 ms |
24568 KB |
Output is correct |
43 |
Correct |
174 ms |
24824 KB |
Output is correct |
44 |
Correct |
3 ms |
384 KB |
Output is correct |
45 |
Correct |
3 ms |
384 KB |
Output is correct |
46 |
Correct |
2 ms |
384 KB |
Output is correct |
47 |
Correct |
4 ms |
512 KB |
Output is correct |
48 |
Correct |
3 ms |
512 KB |
Output is correct |
49 |
Correct |
2 ms |
384 KB |
Output is correct |
50 |
Correct |
3 ms |
384 KB |
Output is correct |
51 |
Incorrect |
3 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '10478', found: '10490' |
52 |
Halted |
0 ms |
0 KB |
- |