#include <bits/stdc++.h>
#include "books.h"
using namespace std;
const int MN=1e6+100;
int mark[MN];
vector<pair<int,pair<int,int> > > e;
int par[MN],l[MN],r[MN];
int root(int x){
return par[x]==x?x:par[x]=root(par[x]);
}
bool merg(int u,int v){
u=root(u),v=root(v);
if(u==v){
return 0;
}
par[v]=u;
return 1;
}
long long minimum_walk(std::vector<int> p, int s) {
int n=p.size();
long long ans=0;
int cnt=0;
for(int i=0;i<n;i++){
ans+=abs(p[i]-i);
if(!mark[i] && p[i]!=i){
int x=i;
do{
mark[x]=cnt;
x=p[x];
}while(x!=i);
cnt++;
}
}
for(int i=0;i<n;i++){
if(p[i]!=i){
for(int j=i+1;j<n;j++){
if(p[j]!=j){
int c;
if(p[i]>j){
c=0;
}
else{
c=j-i;
}
e.push_back({c,{mark[i],mark[j]}});
break;
}
}
}
}
iota(par,par+cnt,0);
sort(e.begin(),e.end());
for(int i=0;i<e.size();i++){
if(merg(e[i].second.first,e[i].second.second)){
ans+=2*e[i].first;
}
}
if(ans){
for(int i=0;i<n;i++){
if(p[i]==i){
ans+=2;
}
else{
break;
}
}
}
return ans;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:54:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<e.size();i++){
~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
4 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
696 KB |
Output is correct |
7 |
Correct |
2 ms |
696 KB |
Output is correct |
8 |
Correct |
3 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
3 ms |
828 KB |
Output is correct |
11 |
Correct |
2 ms |
828 KB |
Output is correct |
12 |
Correct |
2 ms |
828 KB |
Output is correct |
13 |
Correct |
2 ms |
828 KB |
Output is correct |
14 |
Correct |
2 ms |
828 KB |
Output is correct |
15 |
Correct |
3 ms |
828 KB |
Output is correct |
16 |
Correct |
2 ms |
828 KB |
Output is correct |
17 |
Correct |
2 ms |
828 KB |
Output is correct |
18 |
Correct |
2 ms |
828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
4 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
696 KB |
Output is correct |
7 |
Correct |
2 ms |
696 KB |
Output is correct |
8 |
Correct |
3 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
3 ms |
828 KB |
Output is correct |
11 |
Correct |
2 ms |
828 KB |
Output is correct |
12 |
Correct |
2 ms |
828 KB |
Output is correct |
13 |
Correct |
2 ms |
828 KB |
Output is correct |
14 |
Correct |
2 ms |
828 KB |
Output is correct |
15 |
Correct |
3 ms |
828 KB |
Output is correct |
16 |
Correct |
2 ms |
828 KB |
Output is correct |
17 |
Correct |
2 ms |
828 KB |
Output is correct |
18 |
Correct |
2 ms |
828 KB |
Output is correct |
19 |
Correct |
2 ms |
828 KB |
Output is correct |
20 |
Correct |
3 ms |
828 KB |
Output is correct |
21 |
Correct |
3 ms |
828 KB |
Output is correct |
22 |
Correct |
2 ms |
828 KB |
Output is correct |
23 |
Correct |
3 ms |
828 KB |
Output is correct |
24 |
Incorrect |
3 ms |
828 KB |
3rd lines differ - on the 1st token, expected: '2648', found: '2652' |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
4 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
696 KB |
Output is correct |
7 |
Correct |
2 ms |
696 KB |
Output is correct |
8 |
Correct |
3 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
3 ms |
828 KB |
Output is correct |
11 |
Correct |
2 ms |
828 KB |
Output is correct |
12 |
Correct |
2 ms |
828 KB |
Output is correct |
13 |
Correct |
2 ms |
828 KB |
Output is correct |
14 |
Correct |
2 ms |
828 KB |
Output is correct |
15 |
Correct |
3 ms |
828 KB |
Output is correct |
16 |
Correct |
2 ms |
828 KB |
Output is correct |
17 |
Correct |
2 ms |
828 KB |
Output is correct |
18 |
Correct |
2 ms |
828 KB |
Output is correct |
19 |
Correct |
2 ms |
828 KB |
Output is correct |
20 |
Correct |
3 ms |
828 KB |
Output is correct |
21 |
Correct |
3 ms |
828 KB |
Output is correct |
22 |
Correct |
2 ms |
828 KB |
Output is correct |
23 |
Correct |
3 ms |
828 KB |
Output is correct |
24 |
Incorrect |
3 ms |
828 KB |
3rd lines differ - on the 1st token, expected: '2648', found: '2652' |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
828 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4028' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
4 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
696 KB |
Output is correct |
7 |
Correct |
2 ms |
696 KB |
Output is correct |
8 |
Correct |
3 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
3 ms |
828 KB |
Output is correct |
11 |
Correct |
2 ms |
828 KB |
Output is correct |
12 |
Correct |
2 ms |
828 KB |
Output is correct |
13 |
Correct |
2 ms |
828 KB |
Output is correct |
14 |
Correct |
2 ms |
828 KB |
Output is correct |
15 |
Correct |
3 ms |
828 KB |
Output is correct |
16 |
Correct |
2 ms |
828 KB |
Output is correct |
17 |
Correct |
2 ms |
828 KB |
Output is correct |
18 |
Correct |
2 ms |
828 KB |
Output is correct |
19 |
Correct |
2 ms |
828 KB |
Output is correct |
20 |
Correct |
3 ms |
828 KB |
Output is correct |
21 |
Correct |
3 ms |
828 KB |
Output is correct |
22 |
Correct |
2 ms |
828 KB |
Output is correct |
23 |
Correct |
3 ms |
828 KB |
Output is correct |
24 |
Incorrect |
3 ms |
828 KB |
3rd lines differ - on the 1st token, expected: '2648', found: '2652' |
25 |
Halted |
0 ms |
0 KB |
- |