#include "books.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=1e9+7;
const int LIM=1e6+7;
ll F[LIM], mi[LIM], ma[LIM], l, r;
set<ll>S;
ll fnd(ll x) {
if(F[x]==x) return x;
return F[x]=fnd(F[x]);
}
void uni(int a, int b) {
if(fnd(a)==fnd(b)) return;
ma[fnd(a)]=max(ma[fnd(a)], ma[fnd(b)]);
mi[fnd(a)]=min(mi[fnd(a)], mi[fnd(b)]);
F[fnd(b)]=fnd(a);
}
ll solve(ll aktl, ll aktr, ll aktmi, ll aktma) {
if(aktl>aktmi) {
--aktl;
aktmi=min(aktmi, mi[fnd(aktl)]);
aktma=max(aktma, ma[fnd(aktr)]);
return solve(aktl, aktr, aktmi, aktma);
}
if(aktr<aktma) {
++aktr;
aktmi=min(aktmi, mi[fnd(aktr)]);
aktma=max(aktma, ma[fnd(aktr)]);
return solve(aktl, aktr, aktmi, aktma);
}
if(aktl==l && aktr==r) return 0;
ll xl=aktl, xr=aktr, xmi=aktmi, xma=aktma, xans=0;
while(xl>l) {
--xl;
if(xl<xmi) xans+=2;
xmi=min(xmi, mi[fnd(xl)]);
xma=max(xma, ma[fnd(xl)]);
if(xma>xr) break;
}
if(xma==xr && aktl!=l) return solve(xl, xr, xmi, xma)+xans;
ll yl=aktl, yr=aktr, ymi=aktmi, yma=aktma, yans=0;
while(yr<r) {
++yr;
if(yr>yma) yans+=2;
ymi=min(ymi, mi[fnd(yr)]);
yma=max(yma, ma[fnd(yr)]);
if(ymi<yl) break;
}
if(ymi==yl && aktr!=r) return solve(yl, yr, ymi, yma)+yans;
return solve(yl, yr, ymi, yma)+min(xans, yans);
}
ll minimum_walk(vector<int>p, int s) {
ll n=p.size();
rep(i, n) {
F[i]=ma[i]=mi[i]=i;
S.insert(i);
}
rep(i, n) uni(i, p[i]);
vector<pair<pair<ll,ll>,pair<ll,ll>>>V;
l=0; r=n-1;
while(l<s && p[l]==l) ++l;
while(r>s && p[r]==r) --r;
ll ans=0;
rep(i, n) ans+=abs(p[i]-i);
return ans+solve(s, s, mi[fnd(s)], ma[fnd(s)]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
448 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
448 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
452 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
444 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
448 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
452 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
444 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
444 ms |
82740 KB |
Output is correct |
31 |
Correct |
479 ms |
82544 KB |
Output is correct |
32 |
Correct |
293 ms |
82220 KB |
Output is correct |
33 |
Correct |
282 ms |
82072 KB |
Output is correct |
34 |
Correct |
302 ms |
82116 KB |
Output is correct |
35 |
Correct |
299 ms |
82256 KB |
Output is correct |
36 |
Correct |
291 ms |
82156 KB |
Output is correct |
37 |
Correct |
311 ms |
82288 KB |
Output is correct |
38 |
Correct |
296 ms |
82288 KB |
Output is correct |
39 |
Correct |
313 ms |
82512 KB |
Output is correct |
40 |
Correct |
355 ms |
82056 KB |
Output is correct |
41 |
Correct |
330 ms |
82292 KB |
Output is correct |
42 |
Correct |
331 ms |
82080 KB |
Output is correct |
43 |
Correct |
300 ms |
82164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '9800', found: '9802' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
448 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
452 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
444 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
444 ms |
82740 KB |
Output is correct |
31 |
Correct |
479 ms |
82544 KB |
Output is correct |
32 |
Correct |
293 ms |
82220 KB |
Output is correct |
33 |
Correct |
282 ms |
82072 KB |
Output is correct |
34 |
Correct |
302 ms |
82116 KB |
Output is correct |
35 |
Correct |
299 ms |
82256 KB |
Output is correct |
36 |
Correct |
291 ms |
82156 KB |
Output is correct |
37 |
Correct |
311 ms |
82288 KB |
Output is correct |
38 |
Correct |
296 ms |
82288 KB |
Output is correct |
39 |
Correct |
313 ms |
82512 KB |
Output is correct |
40 |
Correct |
355 ms |
82056 KB |
Output is correct |
41 |
Correct |
330 ms |
82292 KB |
Output is correct |
42 |
Correct |
331 ms |
82080 KB |
Output is correct |
43 |
Correct |
300 ms |
82164 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Incorrect |
1 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '9800', found: '9802' |
46 |
Halted |
0 ms |
0 KB |
- |