#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define bug(x) cout << #x << " " << x << endl;
#define ll long long
const ll maxn = 1e6 + 10;
bool marc[maxn], especial[maxn];
int bit[maxn];
vector<int> seg[4*maxn];
queue<int> fila;
void update( int pos, int ini, int fim, int ki, int kf ){
if( ki > fim || ini > kf ) return;
if( ki <= ini && fim <= kf ){ seg[pos].push_back(ki); return; }
int mid = (ini + fim)/2;
update( 2*pos, ini, mid, ki, kf ); update( 2*pos + 1, mid + 1, fim, ki, kf );
}
void activate( int pos, int ini, int fim, int id ){
while( !seg[pos].empty() ){
if( !marc[seg[pos].back()] ){
marc[seg[pos].back()] = true;
fila.push(seg[pos].back());
}
seg[pos].pop_back();
}
if( ini == fim ) return;
int mid = (ini + fim)/2;
if( id <= mid ) activate( 2*pos, ini, mid, id );
else activate( 2*pos + 1, mid + 1, fim, id );
}
long long minimum_walk(vector<int> p, int s) {
if( is_sorted( p.begin(), p.end() ) ) return 0;
ll n = (ll)p.size();
ll resp = 0;
for( ll i = 0; i < (ll)p.size(); i++ ) resp += (ll)abs(p[i] - i);
int maxi = -1;
bool contido = false;
for( ll i = 0; i < n; i++ ) if( !marc[i] && p[i] != i ){
if( i > maxi ){
if( maxi != -1 ) resp += 2LL*(i - maxi);
fila.push(i);
}
int cur = p[i], maior = i;
while( cur != i ){
maxi = max( maxi, cur );
maior = max( maior, cur );
cur = p[cur];
}
update( 1, 0, n - 1, i, maior );
if( i <= s && s <= maxi ) contido = true;
}
while( !fila.empty() ){
int cur = fila.front(); fila.pop();
marc[cur] = true;
while( !especial[cur] ){
especial[cur] = true;
activate( 1, 0, n - 1, cur );
cur = p[cur];
}
}
ll l = s, r = s;
while( l >= 0 && !especial[l] ) l--;
while( r < n && !especial[r] ) r++;
if( contido ) resp += 2LL*min( s - l, r - s );
else{
if( l == -1 ) resp += 2LL*(r - s);
else if( r == n ) resp += 2LL*(s - l);
}
return resp;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
94292 KB |
Output is correct |
2 |
Correct |
44 ms |
94292 KB |
Output is correct |
3 |
Correct |
41 ms |
94296 KB |
Output is correct |
4 |
Correct |
41 ms |
94376 KB |
Output is correct |
5 |
Correct |
42 ms |
94292 KB |
Output is correct |
6 |
Correct |
41 ms |
94336 KB |
Output is correct |
7 |
Correct |
38 ms |
94288 KB |
Output is correct |
8 |
Correct |
45 ms |
94288 KB |
Output is correct |
9 |
Correct |
41 ms |
94300 KB |
Output is correct |
10 |
Correct |
42 ms |
94292 KB |
Output is correct |
11 |
Correct |
40 ms |
94324 KB |
Output is correct |
12 |
Correct |
43 ms |
94288 KB |
Output is correct |
13 |
Correct |
41 ms |
94300 KB |
Output is correct |
14 |
Correct |
42 ms |
94360 KB |
Output is correct |
15 |
Correct |
41 ms |
94288 KB |
Output is correct |
16 |
Correct |
41 ms |
94336 KB |
Output is correct |
17 |
Correct |
45 ms |
94300 KB |
Output is correct |
18 |
Correct |
41 ms |
94288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
94292 KB |
Output is correct |
2 |
Correct |
44 ms |
94292 KB |
Output is correct |
3 |
Correct |
41 ms |
94296 KB |
Output is correct |
4 |
Correct |
41 ms |
94376 KB |
Output is correct |
5 |
Correct |
42 ms |
94292 KB |
Output is correct |
6 |
Correct |
41 ms |
94336 KB |
Output is correct |
7 |
Correct |
38 ms |
94288 KB |
Output is correct |
8 |
Correct |
45 ms |
94288 KB |
Output is correct |
9 |
Correct |
41 ms |
94300 KB |
Output is correct |
10 |
Correct |
42 ms |
94292 KB |
Output is correct |
11 |
Correct |
40 ms |
94324 KB |
Output is correct |
12 |
Correct |
43 ms |
94288 KB |
Output is correct |
13 |
Correct |
41 ms |
94300 KB |
Output is correct |
14 |
Correct |
42 ms |
94360 KB |
Output is correct |
15 |
Correct |
41 ms |
94288 KB |
Output is correct |
16 |
Correct |
41 ms |
94336 KB |
Output is correct |
17 |
Correct |
45 ms |
94300 KB |
Output is correct |
18 |
Correct |
41 ms |
94288 KB |
Output is correct |
19 |
Correct |
42 ms |
94300 KB |
Output is correct |
20 |
Correct |
43 ms |
94292 KB |
Output is correct |
21 |
Correct |
41 ms |
94300 KB |
Output is correct |
22 |
Correct |
42 ms |
94284 KB |
Output is correct |
23 |
Correct |
53 ms |
94516 KB |
Output is correct |
24 |
Correct |
44 ms |
94176 KB |
Output is correct |
25 |
Correct |
41 ms |
94296 KB |
Output is correct |
26 |
Correct |
39 ms |
94296 KB |
Output is correct |
27 |
Correct |
41 ms |
94296 KB |
Output is correct |
28 |
Correct |
41 ms |
94288 KB |
Output is correct |
29 |
Correct |
41 ms |
94292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
94292 KB |
Output is correct |
2 |
Correct |
44 ms |
94292 KB |
Output is correct |
3 |
Correct |
41 ms |
94296 KB |
Output is correct |
4 |
Correct |
41 ms |
94376 KB |
Output is correct |
5 |
Correct |
42 ms |
94292 KB |
Output is correct |
6 |
Correct |
41 ms |
94336 KB |
Output is correct |
7 |
Correct |
38 ms |
94288 KB |
Output is correct |
8 |
Correct |
45 ms |
94288 KB |
Output is correct |
9 |
Correct |
41 ms |
94300 KB |
Output is correct |
10 |
Correct |
42 ms |
94292 KB |
Output is correct |
11 |
Correct |
40 ms |
94324 KB |
Output is correct |
12 |
Correct |
43 ms |
94288 KB |
Output is correct |
13 |
Correct |
41 ms |
94300 KB |
Output is correct |
14 |
Correct |
42 ms |
94360 KB |
Output is correct |
15 |
Correct |
41 ms |
94288 KB |
Output is correct |
16 |
Correct |
41 ms |
94336 KB |
Output is correct |
17 |
Correct |
45 ms |
94300 KB |
Output is correct |
18 |
Correct |
41 ms |
94288 KB |
Output is correct |
19 |
Correct |
42 ms |
94300 KB |
Output is correct |
20 |
Correct |
43 ms |
94292 KB |
Output is correct |
21 |
Correct |
41 ms |
94300 KB |
Output is correct |
22 |
Correct |
42 ms |
94284 KB |
Output is correct |
23 |
Correct |
53 ms |
94516 KB |
Output is correct |
24 |
Correct |
44 ms |
94176 KB |
Output is correct |
25 |
Correct |
41 ms |
94296 KB |
Output is correct |
26 |
Correct |
39 ms |
94296 KB |
Output is correct |
27 |
Correct |
41 ms |
94296 KB |
Output is correct |
28 |
Correct |
41 ms |
94288 KB |
Output is correct |
29 |
Correct |
41 ms |
94292 KB |
Output is correct |
30 |
Execution timed out |
2043 ms |
108884 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
41 ms |
94288 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3590' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
94292 KB |
Output is correct |
2 |
Correct |
44 ms |
94292 KB |
Output is correct |
3 |
Correct |
41 ms |
94296 KB |
Output is correct |
4 |
Correct |
41 ms |
94376 KB |
Output is correct |
5 |
Correct |
42 ms |
94292 KB |
Output is correct |
6 |
Correct |
41 ms |
94336 KB |
Output is correct |
7 |
Correct |
38 ms |
94288 KB |
Output is correct |
8 |
Correct |
45 ms |
94288 KB |
Output is correct |
9 |
Correct |
41 ms |
94300 KB |
Output is correct |
10 |
Correct |
42 ms |
94292 KB |
Output is correct |
11 |
Correct |
40 ms |
94324 KB |
Output is correct |
12 |
Correct |
43 ms |
94288 KB |
Output is correct |
13 |
Correct |
41 ms |
94300 KB |
Output is correct |
14 |
Correct |
42 ms |
94360 KB |
Output is correct |
15 |
Correct |
41 ms |
94288 KB |
Output is correct |
16 |
Correct |
41 ms |
94336 KB |
Output is correct |
17 |
Correct |
45 ms |
94300 KB |
Output is correct |
18 |
Correct |
41 ms |
94288 KB |
Output is correct |
19 |
Correct |
42 ms |
94300 KB |
Output is correct |
20 |
Correct |
43 ms |
94292 KB |
Output is correct |
21 |
Correct |
41 ms |
94300 KB |
Output is correct |
22 |
Correct |
42 ms |
94284 KB |
Output is correct |
23 |
Correct |
53 ms |
94516 KB |
Output is correct |
24 |
Correct |
44 ms |
94176 KB |
Output is correct |
25 |
Correct |
41 ms |
94296 KB |
Output is correct |
26 |
Correct |
39 ms |
94296 KB |
Output is correct |
27 |
Correct |
41 ms |
94296 KB |
Output is correct |
28 |
Correct |
41 ms |
94288 KB |
Output is correct |
29 |
Correct |
41 ms |
94292 KB |
Output is correct |
30 |
Execution timed out |
2043 ms |
108884 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |