// Source: https://oj.uz/problem/view/IOI17_books
//
#include "books.h"
#include "bits/stdc++.h"
using namespace std;
#define s second
#define f first
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
typedef vector<ll> vi;
#define FOR(i, a, b) for (ll i = (a); i<b; i++)
bool ckmin(ll& a, ll b){ return b < a ? a = b, true : false; }
bool ckmax(ll& a, ll b){ return b > a ? a = b, true : false; }
const ll N = 1e6 + 10;
ll ri[N], le[N];
bool vis[N];
vector<int> nxt;
ll n;
ll l,r ;
void extend() {
ll LL = nxt[l];
while (LL < l && l > 0) {
l--;
LL = min(LL, (ll) nxt[l]);
}
ll rr = nxt[r];
while (r < rr && r < n-1) {
r++;
rr = max(rr, (ll)nxt[r]);
}
}
ll minimum_walk(vector<int> p, int s) {
n = p.size();
l = r = s;
nxt = p;
ll ans = 0;
FOR(i, 0, n) ans += abs(i - p[i]);
ll st = 0;
for (;st < n;st++) if (p[st] != st) break;
ll en = n-1;
for (;en >= 0;en--) if (p[en] != en) break;
if (st >= en) return 0;
while (l > st || r < en) {
extend();
// cout << l << r << endl;
ll lo = l;
ll ro = r;
ll cr = 0;
while (l == lo && r < en) {
cr+=2;r++;
// cout << 'd' << endl;
extend();
}
bool rw = (l != lo);
l = lo;r = ro;
ll cl = 0;
while (r == ro && l > st) {
// cout << l << endl;
cl+=2;l--;extend();
}
bool lw = (r != ro);
// assert(lw ^ rw);
if (!lw || !rw) {
ans += cl + cr;
break;
}
ans += min(cl, cr);
}
return ans;
}
// int main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// // FOR(i, 0, 8) {
// // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// // ll n = 100;
// // vi here(n);
// // FOR(i, 0, n) here[i] = i;
// // shuffle(here.begin(), here.end(), rng);
// // FOR(i, 0, n) cout << here[i] << ' ';
// // cout << endl;
// ll res = minimum_walk({0, 2, 4, 5, 1, 6, 3}, 0);
// cout << res << endl;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
76 ms |
12048 KB |
Output is correct |
31 |
Correct |
75 ms |
12024 KB |
Output is correct |
32 |
Correct |
100 ms |
12028 KB |
Output is correct |
33 |
Correct |
85 ms |
12116 KB |
Output is correct |
34 |
Correct |
81 ms |
12184 KB |
Output is correct |
35 |
Correct |
85 ms |
12112 KB |
Output is correct |
36 |
Correct |
77 ms |
11980 KB |
Output is correct |
37 |
Correct |
76 ms |
12116 KB |
Output is correct |
38 |
Correct |
75 ms |
12040 KB |
Output is correct |
39 |
Correct |
80 ms |
12024 KB |
Output is correct |
40 |
Correct |
74 ms |
12116 KB |
Output is correct |
41 |
Correct |
75 ms |
12112 KB |
Output is correct |
42 |
Correct |
95 ms |
12040 KB |
Output is correct |
43 |
Correct |
74 ms |
12024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4076' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
76 ms |
12048 KB |
Output is correct |
31 |
Correct |
75 ms |
12024 KB |
Output is correct |
32 |
Correct |
100 ms |
12028 KB |
Output is correct |
33 |
Correct |
85 ms |
12116 KB |
Output is correct |
34 |
Correct |
81 ms |
12184 KB |
Output is correct |
35 |
Correct |
85 ms |
12112 KB |
Output is correct |
36 |
Correct |
77 ms |
11980 KB |
Output is correct |
37 |
Correct |
76 ms |
12116 KB |
Output is correct |
38 |
Correct |
75 ms |
12040 KB |
Output is correct |
39 |
Correct |
80 ms |
12024 KB |
Output is correct |
40 |
Correct |
74 ms |
12116 KB |
Output is correct |
41 |
Correct |
75 ms |
12112 KB |
Output is correct |
42 |
Correct |
95 ms |
12040 KB |
Output is correct |
43 |
Correct |
74 ms |
12024 KB |
Output is correct |
44 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4076' |
45 |
Halted |
0 ms |
0 KB |
- |