#include "books.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define ryan bear
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef vector<int> vim;
int n; vim P;
int fr, re;
ll ret;
vector<pii> im, ar, fin;
ll minimum_walk(vim p, int s) {
n=p.size();
for (fr=0; fr<n&&p[fr]==fr; fr++) {}
for (re=n-1; re>=0&&p[re]==re; re--) {}
for (int i=0; i<n; i++) ret+=abs(p[i]-i);
if (!ret) return 0;
vim chk(1000010);
int mx;
for (int i=fr; i<=re; i++) {
if (chk[i]) continue;
mx=0;
for (int j=i; !chk[j]; j=p[j]) {
chk[j]=1;
mx=max(mx, j);
}
im.push_back({i, mx});
}
sort(im.begin(), im.end());
for (int i=0; i<im.size(); i++) {
int j;
for (j=i+1; j<im.size()&&im[j].fi<=im[i].se; j++) im[i].se=max(im[i].se, im[j].se);
i=j-1;
}
mx=-1;
for (int i=0; i<im.size(); i++) {
if (mx>=im[i].se) continue;
mx=im[i].se;
ar.push_back(im[i]);
}
for (int i=0; i<ar.size(); i++) {
fin.push_back({ar[i].fi, i});
fin.push_back({ar[i].se, i});
}
sort(fin.begin(), fin.end());
int l;
ll an1=0, an2=0;
for (l=0; l<fin.size(); l++) if (fin[l].fi>=s) break;
an1=abs(fin[l].fi-s)*2;
for (int i=fin[l].se; i<ar.size()-1; i++) an1+=abs(ar[i+1].fi-ar[i].se)*2;
for (int i=fin[l].se; i>0; i--) an1+=abs(ar[i].fi-ar[i-1].se);
if (l==0) return an1+ret;
an2=abs(fin[l-1].se-s)*2;
for (int i=fin[l-1].se; i>0; i--) an2+=abs(ar[i].fi-ar[i-1].se);
for (int i=fin[l-1].se; i<ar.size()-1; i++) an2+=abs(ar[i+1].fi-ar[i].se)*2;
return min(an1, an2)+ret;
}
Compilation message
books.cpp: In function 'll minimum_walk(vim, int)':
books.cpp:41:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<im.size(); i++) {
~^~~~~~~~~~
books.cpp:43:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j=i+1; j<im.size()&&im[j].fi<=im[i].se; j++) im[i].se=max(im[i].se, im[j].se);
~^~~~~~~~~~
books.cpp:47:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<im.size(); i++) {
~^~~~~~~~~~
books.cpp:54:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<ar.size(); i++) {
~^~~~~~~~~~
books.cpp:61:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (l=0; l<fin.size(); l++) if (fin[l].fi>=s) break;
~^~~~~~~~~~~
books.cpp:65:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=fin[l].se; i<ar.size()-1; i++) an1+=abs(ar[i+1].fi-ar[i].se)*2;
~^~~~~~~~~~~~
books.cpp:72:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=fin[l-1].se; i<ar.size()-1; i++) an2+=abs(ar[i+1].fi-ar[i].se)*2;
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
6 ms |
4344 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
5 ms |
4220 KB |
Output is correct |
8 |
Correct |
5 ms |
4216 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
4216 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
4216 KB |
Output is correct |
13 |
Correct |
5 ms |
4216 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
4216 KB |
Output is correct |
16 |
Correct |
5 ms |
4260 KB |
Output is correct |
17 |
Correct |
5 ms |
4216 KB |
Output is correct |
18 |
Correct |
5 ms |
4216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
6 ms |
4344 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
5 ms |
4220 KB |
Output is correct |
8 |
Correct |
5 ms |
4216 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
4216 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
4216 KB |
Output is correct |
13 |
Correct |
5 ms |
4216 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
4216 KB |
Output is correct |
16 |
Correct |
5 ms |
4260 KB |
Output is correct |
17 |
Correct |
5 ms |
4216 KB |
Output is correct |
18 |
Correct |
5 ms |
4216 KB |
Output is correct |
19 |
Correct |
5 ms |
4216 KB |
Output is correct |
20 |
Correct |
5 ms |
4216 KB |
Output is correct |
21 |
Correct |
2 ms |
256 KB |
Output is correct |
22 |
Correct |
6 ms |
4344 KB |
Output is correct |
23 |
Correct |
7 ms |
4216 KB |
Output is correct |
24 |
Correct |
6 ms |
4268 KB |
Output is correct |
25 |
Correct |
5 ms |
4216 KB |
Output is correct |
26 |
Correct |
6 ms |
4216 KB |
Output is correct |
27 |
Correct |
5 ms |
4216 KB |
Output is correct |
28 |
Correct |
6 ms |
4216 KB |
Output is correct |
29 |
Correct |
6 ms |
4216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
6 ms |
4344 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
5 ms |
4220 KB |
Output is correct |
8 |
Correct |
5 ms |
4216 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
4216 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
4216 KB |
Output is correct |
13 |
Correct |
5 ms |
4216 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
4216 KB |
Output is correct |
16 |
Correct |
5 ms |
4260 KB |
Output is correct |
17 |
Correct |
5 ms |
4216 KB |
Output is correct |
18 |
Correct |
5 ms |
4216 KB |
Output is correct |
19 |
Correct |
5 ms |
4216 KB |
Output is correct |
20 |
Correct |
5 ms |
4216 KB |
Output is correct |
21 |
Correct |
2 ms |
256 KB |
Output is correct |
22 |
Correct |
6 ms |
4344 KB |
Output is correct |
23 |
Correct |
7 ms |
4216 KB |
Output is correct |
24 |
Correct |
6 ms |
4268 KB |
Output is correct |
25 |
Correct |
5 ms |
4216 KB |
Output is correct |
26 |
Correct |
6 ms |
4216 KB |
Output is correct |
27 |
Correct |
5 ms |
4216 KB |
Output is correct |
28 |
Correct |
6 ms |
4216 KB |
Output is correct |
29 |
Correct |
6 ms |
4216 KB |
Output is correct |
30 |
Correct |
211 ms |
12152 KB |
Output is correct |
31 |
Correct |
198 ms |
12152 KB |
Output is correct |
32 |
Correct |
135 ms |
8156 KB |
Output is correct |
33 |
Correct |
255 ms |
39092 KB |
Output is correct |
34 |
Correct |
255 ms |
39104 KB |
Output is correct |
35 |
Correct |
222 ms |
35284 KB |
Output is correct |
36 |
Correct |
176 ms |
19296 KB |
Output is correct |
37 |
Correct |
152 ms |
12788 KB |
Output is correct |
38 |
Correct |
154 ms |
12220 KB |
Output is correct |
39 |
Correct |
154 ms |
12220 KB |
Output is correct |
40 |
Correct |
167 ms |
12096 KB |
Output is correct |
41 |
Correct |
200 ms |
12124 KB |
Output is correct |
42 |
Correct |
186 ms |
12152 KB |
Output is correct |
43 |
Correct |
181 ms |
20396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
4216 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
6 ms |
4344 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
5 ms |
4220 KB |
Output is correct |
8 |
Correct |
5 ms |
4216 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
4216 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
4216 KB |
Output is correct |
13 |
Correct |
5 ms |
4216 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
4216 KB |
Output is correct |
16 |
Correct |
5 ms |
4260 KB |
Output is correct |
17 |
Correct |
5 ms |
4216 KB |
Output is correct |
18 |
Correct |
5 ms |
4216 KB |
Output is correct |
19 |
Correct |
5 ms |
4216 KB |
Output is correct |
20 |
Correct |
5 ms |
4216 KB |
Output is correct |
21 |
Correct |
2 ms |
256 KB |
Output is correct |
22 |
Correct |
6 ms |
4344 KB |
Output is correct |
23 |
Correct |
7 ms |
4216 KB |
Output is correct |
24 |
Correct |
6 ms |
4268 KB |
Output is correct |
25 |
Correct |
5 ms |
4216 KB |
Output is correct |
26 |
Correct |
6 ms |
4216 KB |
Output is correct |
27 |
Correct |
5 ms |
4216 KB |
Output is correct |
28 |
Correct |
6 ms |
4216 KB |
Output is correct |
29 |
Correct |
6 ms |
4216 KB |
Output is correct |
30 |
Correct |
211 ms |
12152 KB |
Output is correct |
31 |
Correct |
198 ms |
12152 KB |
Output is correct |
32 |
Correct |
135 ms |
8156 KB |
Output is correct |
33 |
Correct |
255 ms |
39092 KB |
Output is correct |
34 |
Correct |
255 ms |
39104 KB |
Output is correct |
35 |
Correct |
222 ms |
35284 KB |
Output is correct |
36 |
Correct |
176 ms |
19296 KB |
Output is correct |
37 |
Correct |
152 ms |
12788 KB |
Output is correct |
38 |
Correct |
154 ms |
12220 KB |
Output is correct |
39 |
Correct |
154 ms |
12220 KB |
Output is correct |
40 |
Correct |
167 ms |
12096 KB |
Output is correct |
41 |
Correct |
200 ms |
12124 KB |
Output is correct |
42 |
Correct |
186 ms |
12152 KB |
Output is correct |
43 |
Correct |
181 ms |
20396 KB |
Output is correct |
44 |
Incorrect |
5 ms |
4216 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
45 |
Halted |
0 ms |
0 KB |
- |