#include "books.h"
#include <bits/stdc++.h>
#define MAX_N 1000000
using namespace std;
struct cikl{
int l, r;//Najlevlji i najdesnji
long long sum;//suma p_i-i na ciklusu.
};
long long aps(int a){
return a>0?a:-1*a;
}
vector<cikl> C;
int numcyc=0, numcom=0;//broj ciklova
int N;
long long ans=0;
bool mark[MAX_N];
//vector<int> p;
long long minimum_walk(vector<int> p, int s){
//Ako je s=0 i n<1000
N=p.size();
while(N && p[N-1]==N-1){
N--;//Ovaj deo mi ionako ne treba
}
int nula=0;
while(nula<N && p[nula]==nula){
nula++;
}
if(N==nula)return 0;
for(int i=nula; i<N; ++i){
if(!mark[i]){
numcyc++;
cikl A;
A.l=A.r=i;
A.sum=0;
int cur=i;
do{
cur=p[cur];
if(cur>A.r)A.r=cur;
if(cur<A.l)A.l=cur;
A.sum+=aps(p[cur]-cur);
mark[cur]=1;
}while(i!=cur);
C.push_back(A);
//Ovako formiran niz ciklova je sortiran prema levom kraju--koji je uvek i
}
}
for(int i=0; i<numcyc; ++i){
//printf("C[%d]:%d %d %d;\n", i, C[i].l, C[i].r, C[i].sum);
}
int scom=0, lcom=nula, rcom=nula;
for(int i=0; i<numcyc; ++i){
ans+=C[i].sum;
if(C[i].l<=rcom){
//Mogu da ga uhvatim, i dalje sam u istoj komponenti
}
else{
if(s>=lcom && s<=rcom)//Menjam komponentu
scom=numcom;
numcom++;
lcom=C[i].l;
}
if(C[i].r>rcom)
rcom=C[i].r;
}
//printf("%d\n", ans);
ans+=2*numcom;
int mini=N;
for(int i=nula; i<N; ++i){
if((p[i]!=i) && (abs(s-i)<mini)){
//printf("Updatujem i=%d mini=%d s=%d\n", i, mini, s);
mini=abs(s-i);
}
}
return ans+2*mini;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:54:6: warning: variable 'scom' set but not used [-Wunused-but-set-variable]
int scom=0, lcom=nula, rcom=nula;
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
424 KB |
Output is correct |
4 |
Correct |
2 ms |
424 KB |
Output is correct |
5 |
Correct |
2 ms |
424 KB |
Output is correct |
6 |
Correct |
2 ms |
448 KB |
Output is correct |
7 |
Correct |
2 ms |
524 KB |
Output is correct |
8 |
Correct |
2 ms |
576 KB |
Output is correct |
9 |
Correct |
3 ms |
576 KB |
Output is correct |
10 |
Correct |
3 ms |
592 KB |
Output is correct |
11 |
Correct |
3 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
636 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
2 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
424 KB |
Output is correct |
4 |
Correct |
2 ms |
424 KB |
Output is correct |
5 |
Correct |
2 ms |
424 KB |
Output is correct |
6 |
Correct |
2 ms |
448 KB |
Output is correct |
7 |
Correct |
2 ms |
524 KB |
Output is correct |
8 |
Correct |
2 ms |
576 KB |
Output is correct |
9 |
Correct |
3 ms |
576 KB |
Output is correct |
10 |
Correct |
3 ms |
592 KB |
Output is correct |
11 |
Correct |
3 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
636 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
2 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
660 KB |
Output is correct |
24 |
Correct |
3 ms |
660 KB |
Output is correct |
25 |
Correct |
3 ms |
660 KB |
Output is correct |
26 |
Correct |
2 ms |
660 KB |
Output is correct |
27 |
Correct |
2 ms |
660 KB |
Output is correct |
28 |
Correct |
2 ms |
660 KB |
Output is correct |
29 |
Correct |
2 ms |
660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
424 KB |
Output is correct |
4 |
Correct |
2 ms |
424 KB |
Output is correct |
5 |
Correct |
2 ms |
424 KB |
Output is correct |
6 |
Correct |
2 ms |
448 KB |
Output is correct |
7 |
Correct |
2 ms |
524 KB |
Output is correct |
8 |
Correct |
2 ms |
576 KB |
Output is correct |
9 |
Correct |
3 ms |
576 KB |
Output is correct |
10 |
Correct |
3 ms |
592 KB |
Output is correct |
11 |
Correct |
3 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
636 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
2 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
660 KB |
Output is correct |
24 |
Correct |
3 ms |
660 KB |
Output is correct |
25 |
Correct |
3 ms |
660 KB |
Output is correct |
26 |
Correct |
2 ms |
660 KB |
Output is correct |
27 |
Correct |
2 ms |
660 KB |
Output is correct |
28 |
Correct |
2 ms |
660 KB |
Output is correct |
29 |
Correct |
2 ms |
660 KB |
Output is correct |
30 |
Correct |
281 ms |
9420 KB |
Output is correct |
31 |
Correct |
218 ms |
9420 KB |
Output is correct |
32 |
Correct |
135 ms |
9420 KB |
Output is correct |
33 |
Correct |
175 ms |
25640 KB |
Output is correct |
34 |
Correct |
188 ms |
25816 KB |
Output is correct |
35 |
Correct |
177 ms |
25944 KB |
Output is correct |
36 |
Correct |
168 ms |
25944 KB |
Output is correct |
37 |
Correct |
162 ms |
25944 KB |
Output is correct |
38 |
Correct |
220 ms |
25944 KB |
Output is correct |
39 |
Correct |
186 ms |
25944 KB |
Output is correct |
40 |
Correct |
183 ms |
25944 KB |
Output is correct |
41 |
Correct |
202 ms |
25944 KB |
Output is correct |
42 |
Correct |
211 ms |
25944 KB |
Output is correct |
43 |
Correct |
185 ms |
25944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
25944 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 |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
424 KB |
Output is correct |
4 |
Correct |
2 ms |
424 KB |
Output is correct |
5 |
Correct |
2 ms |
424 KB |
Output is correct |
6 |
Correct |
2 ms |
448 KB |
Output is correct |
7 |
Correct |
2 ms |
524 KB |
Output is correct |
8 |
Correct |
2 ms |
576 KB |
Output is correct |
9 |
Correct |
3 ms |
576 KB |
Output is correct |
10 |
Correct |
3 ms |
592 KB |
Output is correct |
11 |
Correct |
3 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
636 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
2 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
660 KB |
Output is correct |
24 |
Correct |
3 ms |
660 KB |
Output is correct |
25 |
Correct |
3 ms |
660 KB |
Output is correct |
26 |
Correct |
2 ms |
660 KB |
Output is correct |
27 |
Correct |
2 ms |
660 KB |
Output is correct |
28 |
Correct |
2 ms |
660 KB |
Output is correct |
29 |
Correct |
2 ms |
660 KB |
Output is correct |
30 |
Correct |
281 ms |
9420 KB |
Output is correct |
31 |
Correct |
218 ms |
9420 KB |
Output is correct |
32 |
Correct |
135 ms |
9420 KB |
Output is correct |
33 |
Correct |
175 ms |
25640 KB |
Output is correct |
34 |
Correct |
188 ms |
25816 KB |
Output is correct |
35 |
Correct |
177 ms |
25944 KB |
Output is correct |
36 |
Correct |
168 ms |
25944 KB |
Output is correct |
37 |
Correct |
162 ms |
25944 KB |
Output is correct |
38 |
Correct |
220 ms |
25944 KB |
Output is correct |
39 |
Correct |
186 ms |
25944 KB |
Output is correct |
40 |
Correct |
183 ms |
25944 KB |
Output is correct |
41 |
Correct |
202 ms |
25944 KB |
Output is correct |
42 |
Correct |
211 ms |
25944 KB |
Output is correct |
43 |
Correct |
185 ms |
25944 KB |
Output is correct |
44 |
Incorrect |
3 ms |
25944 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |