#include "books.h"
#include <bits/stdc++.h>
#define debug printf
#define lp(i,a,b) for(int i = a ; i < b; i++)
#define pb push_back
#define ff first
#define ss second
#define mk make_pair
#define pii pair<int,int>
#define ll long long
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
const int MAX = 1e6+10 ;
using namespace std ;
//Tentando uma ideia (provavelmente vai dar errado mas vamos nessa)
long long minimum_walk(vector<int> p, int s)
{
int n = sz(p) ;
ll ans = 0LL ;
lp(i,0,n) ans += (ll)(abs(p[i]-i)) ;
int L = 0 ;
int R = n-1 ;
while(L < s && p[L] == L ) L++ ;
while(R > s && p[R] == R) R-- ;
vector<bool> vis( n, false ) ;
vector<pii> intervalo(n, make_pair(-1,-1) ) ;
for(int i = L ; i <= R ; i++ )
{
if(vis[i]) continue ;
vector<int> List ;
int x = i , l = i , r = i;
while(!vis[x])
{
vis[x] = true ;
List.push_back(x) ;
l = min(l,x) ;
r = max(r,x) ;
x = p[x] ;
}
for(auto e : List ) intervalo[e] = make_pair(l,r) ;
}
int l = s , r = s ;
int lmin = intervalo[s].ff, rmax = intervalo[s].ss ;
ll lastTime = ans ;
while( !(l <= L && r >= R) )
{
while( l > lmin || r < rmax )
{
if( l > lmin )
{
l-- ;
if( intervalo[l].ff <= lmin && intervalo[l].ss >= rmax )
lastTime = ans ;
lmin = min( lmin , intervalo[l].ff ) ;
rmax = max(rmax, intervalo[l].ss ) ;
}
if(r < rmax )
{
r++ ;
if(intervalo[r].ff <= lmin && intervalo[r].ss >= rmax)
lastTime = ans ;
lmin = min(lmin, intervalo[r].ff );
rmax = max(rmax , intervalo[r].ss ) ;
}
}
//Agora custa
if( l <= L && r >= R ) break ;
int auxl= lmin ;
int auxr = rmax ;
int mnr = auxr, mxl = auxl ;
int cost = 0 ;
bool ok = false ;
while( auxl > L && auxr < R )
{
cost += 2LL ;
auxl-- ;
auxr++ ;
for(int i = auxl ; i >= intervalo[auxl].ff ; i--)
{
auxl = min(auxl, intervalo[i].ff ) ;
mxl = max(mxl, intervalo[i].ss ) ;
}
for(int i = auxr ; i <= intervalo[auxr].ss ; i++ )
{
auxr = max(auxr, intervalo[i].ss ) ;
mnr = min(mnr, intervalo[i].ff ) ;
}
if(mnr < lmin || mxl > rmax )
{
lmin = mnr ;
rmax = mxl ;
ans += cost ;
ok = true ;
break ;
}
}
if(!ok) break ;
}
while( l > L )
{
ans += 2LL ;
l-- ;
for(int i = l ; i >= intervalo[l].ff ; i-- )
l = min(l, intervalo[i].ff ) ;
}
while(r < R)
{
ans += 2LL ;
r++ ;
for(int i = r ; i <= intervalo[r].ss ; i++ )
r = max(r, intervalo[i].ss ) ;
}
return ans ;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:63:5: warning: variable 'lastTime' set but not used [-Wunused-but-set-variable]
63 | ll lastTime = ans ;
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
256 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
256 KB |
Output is correct |
26 |
Correct |
1 ms |
256 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
256 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
256 KB |
Output is correct |
26 |
Correct |
1 ms |
256 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
228 ms |
20400 KB |
Output is correct |
31 |
Correct |
229 ms |
18300 KB |
Output is correct |
32 |
Correct |
137 ms |
16120 KB |
Output is correct |
33 |
Correct |
199 ms |
16120 KB |
Output is correct |
34 |
Correct |
201 ms |
16120 KB |
Output is correct |
35 |
Correct |
199 ms |
16120 KB |
Output is correct |
36 |
Correct |
188 ms |
16120 KB |
Output is correct |
37 |
Correct |
168 ms |
16120 KB |
Output is correct |
38 |
Correct |
172 ms |
16192 KB |
Output is correct |
39 |
Correct |
167 ms |
16120 KB |
Output is correct |
40 |
Correct |
177 ms |
16560 KB |
Output is correct |
41 |
Correct |
202 ms |
19192 KB |
Output is correct |
42 |
Correct |
192 ms |
17824 KB |
Output is correct |
43 |
Correct |
195 ms |
16120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
256 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
256 KB |
Output is correct |
26 |
Correct |
1 ms |
256 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
228 ms |
20400 KB |
Output is correct |
31 |
Correct |
229 ms |
18300 KB |
Output is correct |
32 |
Correct |
137 ms |
16120 KB |
Output is correct |
33 |
Correct |
199 ms |
16120 KB |
Output is correct |
34 |
Correct |
201 ms |
16120 KB |
Output is correct |
35 |
Correct |
199 ms |
16120 KB |
Output is correct |
36 |
Correct |
188 ms |
16120 KB |
Output is correct |
37 |
Correct |
168 ms |
16120 KB |
Output is correct |
38 |
Correct |
172 ms |
16192 KB |
Output is correct |
39 |
Correct |
167 ms |
16120 KB |
Output is correct |
40 |
Correct |
177 ms |
16560 KB |
Output is correct |
41 |
Correct |
202 ms |
19192 KB |
Output is correct |
42 |
Correct |
192 ms |
17824 KB |
Output is correct |
43 |
Correct |
195 ms |
16120 KB |
Output is correct |
44 |
Correct |
1 ms |
256 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
256 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |
50 |
Correct |
1 ms |
384 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
1 ms |
384 KB |
Output is correct |
53 |
Correct |
1 ms |
384 KB |
Output is correct |
54 |
Correct |
1 ms |
384 KB |
Output is correct |
55 |
Correct |
1 ms |
384 KB |
Output is correct |
56 |
Correct |
1 ms |
384 KB |
Output is correct |
57 |
Correct |
1 ms |
384 KB |
Output is correct |
58 |
Correct |
1 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
1 ms |
384 KB |
Output is correct |
62 |
Correct |
1 ms |
384 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
64 |
Correct |
199 ms |
22904 KB |
Output is correct |
65 |
Correct |
193 ms |
22776 KB |
Output is correct |
66 |
Correct |
176 ms |
22904 KB |
Output is correct |
67 |
Correct |
180 ms |
22904 KB |
Output is correct |
68 |
Correct |
19 ms |
2560 KB |
Output is correct |
69 |
Correct |
19 ms |
2688 KB |
Output is correct |
70 |
Correct |
19 ms |
2432 KB |
Output is correct |
71 |
Correct |
20 ms |
2432 KB |
Output is correct |
72 |
Correct |
20 ms |
2432 KB |
Output is correct |
73 |
Correct |
17 ms |
2560 KB |
Output is correct |
74 |
Correct |
210 ms |
22904 KB |
Output is correct |
75 |
Correct |
208 ms |
22904 KB |
Output is correct |
76 |
Correct |
201 ms |
22904 KB |
Output is correct |
77 |
Correct |
204 ms |
22908 KB |
Output is correct |
78 |
Correct |
209 ms |
22776 KB |
Output is correct |
79 |
Correct |
203 ms |
22776 KB |
Output is correct |
80 |
Correct |
147 ms |
22776 KB |
Output is correct |
81 |
Correct |
196 ms |
22904 KB |
Output is correct |
82 |
Correct |
196 ms |
22848 KB |
Output is correct |
83 |
Correct |
202 ms |
22776 KB |
Output is correct |
84 |
Correct |
198 ms |
22780 KB |
Output is correct |
85 |
Correct |
187 ms |
22776 KB |
Output is correct |
86 |
Correct |
177 ms |
22904 KB |
Output is correct |
87 |
Correct |
199 ms |
22776 KB |
Output is correct |
88 |
Correct |
203 ms |
22904 KB |
Output is correct |
89 |
Correct |
202 ms |
22776 KB |
Output is correct |
90 |
Correct |
179 ms |
22852 KB |
Output is correct |
91 |
Correct |
175 ms |
22904 KB |
Output is correct |
92 |
Correct |
178 ms |
23032 KB |
Output is correct |