#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> point;
void debug_out(){cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
cerr << H << ' ';
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
const int maxn = 1e6 + 10;
const ll inf = 1e18;
int n, mn, mx, cnt[maxn][2];
ll Calc(){
ll ans = 0;
for (int i = mn+1; i <= mx; i++){
cnt[i][0] += cnt[i-1][0];
cnt[i][1] += cnt[i-1][1];
//debug(i, cnt[i][0], cnt[i][1]);
if (cnt[i][0] == 0){
cnt[i][0]++;
cnt[i+1][0]--;
}
if (cnt[i][1] == 0){
cnt[i][1]++;
cnt[i+1][1]--;
}
//debug(i, cnt[i][0], cnt[i][1]);
ans += 2 * max(cnt[i][0], cnt[i][1]);
}
return ans;
}
ll minimum_walk(vector<int> p, int s) {
n = p.size();
mn = s, mx = s;
int mxmn = -1, mnmx = n+1;
for (int i = 0; i < n; i++){
if (p[i] == i) continue;
mn = min(mn, i);
if (i <= s) mxmn = max(mxmn, i);
mx = max(mx, i);
if (s <= i) mnmx = min(mnmx, i);
if (p[i] < i){
cnt[p[i]+1][0]++;
cnt[i+1][0]--;
}
else{
cnt[i+1][1]++;
cnt[p[i]+1][1]--;
}
}
//debug(mn, mx);
if (mn == mx) return 0;
//debug(1);
if (mxmn == -1) mxmn = s;
if (mnmx == n+1) mnmx = s;
//debug(mxmn, mnmx);
ll ans = inf;
for (int j = 0; j < 4; j++){
memset(cnt, 0, sizeof cnt);
if (j == 0){
cnt[mxmn+1][0]++;
cnt[mxmn+1][1]++;
cnt[s+1][0]--;
cnt[s+1][1]--;
}
else if (j == 1){
cnt[mnmx+1][0]--;
cnt[mnmx+1][1]--;
cnt[s+1][0]++;
cnt[s+1][1]++;
}
else if (j == 2){
cnt[mxmn+1][1]++;
cnt[s+1][1]--;
cnt[s+1][1]++;
cnt[mnmx+1][1]--;
}
else{
cnt[mxmn+1][0]++;
cnt[s+1][0]--;
cnt[s+1][0]++;
cnt[mnmx+1][0]--;
}
for (int i = 0; i < n; i++){
if (p[i] == i) continue;
if (p[i] < i){
cnt[p[i]+1][0]++;
cnt[i+1][0]--;
}
else{
cnt[i+1][1]++;
cnt[p[i]+1][1]--;
}
}
ans = min(ans, Calc());
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8020 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8020 KB |
Output is correct |
4 |
Correct |
4 ms |
8020 KB |
Output is correct |
5 |
Correct |
5 ms |
8020 KB |
Output is correct |
6 |
Correct |
4 ms |
8020 KB |
Output is correct |
7 |
Correct |
4 ms |
8020 KB |
Output is correct |
8 |
Correct |
4 ms |
8116 KB |
Output is correct |
9 |
Correct |
1 ms |
232 KB |
Output is correct |
10 |
Correct |
4 ms |
8020 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
8020 KB |
Output is correct |
13 |
Correct |
4 ms |
8020 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
4 ms |
8116 KB |
Output is correct |
16 |
Correct |
4 ms |
8032 KB |
Output is correct |
17 |
Correct |
4 ms |
8020 KB |
Output is correct |
18 |
Correct |
4 ms |
8020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8020 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8020 KB |
Output is correct |
4 |
Correct |
4 ms |
8020 KB |
Output is correct |
5 |
Correct |
5 ms |
8020 KB |
Output is correct |
6 |
Correct |
4 ms |
8020 KB |
Output is correct |
7 |
Correct |
4 ms |
8020 KB |
Output is correct |
8 |
Correct |
4 ms |
8116 KB |
Output is correct |
9 |
Correct |
1 ms |
232 KB |
Output is correct |
10 |
Correct |
4 ms |
8020 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
8020 KB |
Output is correct |
13 |
Correct |
4 ms |
8020 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
4 ms |
8116 KB |
Output is correct |
16 |
Correct |
4 ms |
8032 KB |
Output is correct |
17 |
Correct |
4 ms |
8020 KB |
Output is correct |
18 |
Correct |
4 ms |
8020 KB |
Output is correct |
19 |
Correct |
4 ms |
8020 KB |
Output is correct |
20 |
Correct |
4 ms |
8020 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
4 ms |
8020 KB |
Output is correct |
23 |
Correct |
5 ms |
8112 KB |
Output is correct |
24 |
Correct |
4 ms |
8020 KB |
Output is correct |
25 |
Correct |
4 ms |
8120 KB |
Output is correct |
26 |
Correct |
4 ms |
8020 KB |
Output is correct |
27 |
Correct |
4 ms |
8020 KB |
Output is correct |
28 |
Correct |
4 ms |
8020 KB |
Output is correct |
29 |
Correct |
4 ms |
8020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8020 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8020 KB |
Output is correct |
4 |
Correct |
4 ms |
8020 KB |
Output is correct |
5 |
Correct |
5 ms |
8020 KB |
Output is correct |
6 |
Correct |
4 ms |
8020 KB |
Output is correct |
7 |
Correct |
4 ms |
8020 KB |
Output is correct |
8 |
Correct |
4 ms |
8116 KB |
Output is correct |
9 |
Correct |
1 ms |
232 KB |
Output is correct |
10 |
Correct |
4 ms |
8020 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
8020 KB |
Output is correct |
13 |
Correct |
4 ms |
8020 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
4 ms |
8116 KB |
Output is correct |
16 |
Correct |
4 ms |
8032 KB |
Output is correct |
17 |
Correct |
4 ms |
8020 KB |
Output is correct |
18 |
Correct |
4 ms |
8020 KB |
Output is correct |
19 |
Correct |
4 ms |
8020 KB |
Output is correct |
20 |
Correct |
4 ms |
8020 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
4 ms |
8020 KB |
Output is correct |
23 |
Correct |
5 ms |
8112 KB |
Output is correct |
24 |
Correct |
4 ms |
8020 KB |
Output is correct |
25 |
Correct |
4 ms |
8120 KB |
Output is correct |
26 |
Correct |
4 ms |
8020 KB |
Output is correct |
27 |
Correct |
4 ms |
8020 KB |
Output is correct |
28 |
Correct |
4 ms |
8020 KB |
Output is correct |
29 |
Correct |
4 ms |
8020 KB |
Output is correct |
30 |
Correct |
109 ms |
16328 KB |
Output is correct |
31 |
Correct |
136 ms |
16436 KB |
Output is correct |
32 |
Correct |
67 ms |
8468 KB |
Output is correct |
33 |
Correct |
116 ms |
16412 KB |
Output is correct |
34 |
Correct |
115 ms |
16324 KB |
Output is correct |
35 |
Correct |
124 ms |
16336 KB |
Output is correct |
36 |
Correct |
126 ms |
16236 KB |
Output is correct |
37 |
Correct |
106 ms |
16260 KB |
Output is correct |
38 |
Correct |
101 ms |
16220 KB |
Output is correct |
39 |
Correct |
102 ms |
16224 KB |
Output is correct |
40 |
Correct |
102 ms |
16204 KB |
Output is correct |
41 |
Correct |
116 ms |
16304 KB |
Output is correct |
42 |
Correct |
117 ms |
16256 KB |
Output is correct |
43 |
Correct |
110 ms |
16328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
8020 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8020 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8020 KB |
Output is correct |
4 |
Correct |
4 ms |
8020 KB |
Output is correct |
5 |
Correct |
5 ms |
8020 KB |
Output is correct |
6 |
Correct |
4 ms |
8020 KB |
Output is correct |
7 |
Correct |
4 ms |
8020 KB |
Output is correct |
8 |
Correct |
4 ms |
8116 KB |
Output is correct |
9 |
Correct |
1 ms |
232 KB |
Output is correct |
10 |
Correct |
4 ms |
8020 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
8020 KB |
Output is correct |
13 |
Correct |
4 ms |
8020 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
4 ms |
8116 KB |
Output is correct |
16 |
Correct |
4 ms |
8032 KB |
Output is correct |
17 |
Correct |
4 ms |
8020 KB |
Output is correct |
18 |
Correct |
4 ms |
8020 KB |
Output is correct |
19 |
Correct |
4 ms |
8020 KB |
Output is correct |
20 |
Correct |
4 ms |
8020 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
4 ms |
8020 KB |
Output is correct |
23 |
Correct |
5 ms |
8112 KB |
Output is correct |
24 |
Correct |
4 ms |
8020 KB |
Output is correct |
25 |
Correct |
4 ms |
8120 KB |
Output is correct |
26 |
Correct |
4 ms |
8020 KB |
Output is correct |
27 |
Correct |
4 ms |
8020 KB |
Output is correct |
28 |
Correct |
4 ms |
8020 KB |
Output is correct |
29 |
Correct |
4 ms |
8020 KB |
Output is correct |
30 |
Correct |
109 ms |
16328 KB |
Output is correct |
31 |
Correct |
136 ms |
16436 KB |
Output is correct |
32 |
Correct |
67 ms |
8468 KB |
Output is correct |
33 |
Correct |
116 ms |
16412 KB |
Output is correct |
34 |
Correct |
115 ms |
16324 KB |
Output is correct |
35 |
Correct |
124 ms |
16336 KB |
Output is correct |
36 |
Correct |
126 ms |
16236 KB |
Output is correct |
37 |
Correct |
106 ms |
16260 KB |
Output is correct |
38 |
Correct |
101 ms |
16220 KB |
Output is correct |
39 |
Correct |
102 ms |
16224 KB |
Output is correct |
40 |
Correct |
102 ms |
16204 KB |
Output is correct |
41 |
Correct |
116 ms |
16304 KB |
Output is correct |
42 |
Correct |
117 ms |
16256 KB |
Output is correct |
43 |
Correct |
110 ms |
16328 KB |
Output is correct |
44 |
Incorrect |
4 ms |
8020 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |