#include <bits/stdc++.h>
#include "books.h"
using namespace std;
const int MN=1e5+100;
int mark[MN];
vector<pair<int,pair<int,int> > > e;
int par[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){
e.push_back({j-i,{mark[i],mark[j]}});
break;
}
}
}
}
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:46:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<e.size();i++){
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
508 KB |
Output is correct |
5 |
Correct |
4 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
508 KB |
Output is correct |
7 |
Correct |
2 ms |
508 KB |
Output is correct |
8 |
Correct |
3 ms |
544 KB |
Output is correct |
9 |
Correct |
3 ms |
544 KB |
Output is correct |
10 |
Correct |
4 ms |
544 KB |
Output is correct |
11 |
Correct |
3 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
3 ms |
628 KB |
Output is correct |
16 |
Correct |
2 ms |
628 KB |
Output is correct |
17 |
Correct |
3 ms |
636 KB |
Output is correct |
18 |
Correct |
3 ms |
636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
508 KB |
Output is correct |
5 |
Correct |
4 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
508 KB |
Output is correct |
7 |
Correct |
2 ms |
508 KB |
Output is correct |
8 |
Correct |
3 ms |
544 KB |
Output is correct |
9 |
Correct |
3 ms |
544 KB |
Output is correct |
10 |
Correct |
4 ms |
544 KB |
Output is correct |
11 |
Correct |
3 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
3 ms |
628 KB |
Output is correct |
16 |
Correct |
2 ms |
628 KB |
Output is correct |
17 |
Correct |
3 ms |
636 KB |
Output is correct |
18 |
Correct |
3 ms |
636 KB |
Output is correct |
19 |
Correct |
2 ms |
636 KB |
Output is correct |
20 |
Correct |
3 ms |
636 KB |
Output is correct |
21 |
Correct |
2 ms |
636 KB |
Output is correct |
22 |
Incorrect |
2 ms |
636 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '788' |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
508 KB |
Output is correct |
5 |
Correct |
4 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
508 KB |
Output is correct |
7 |
Correct |
2 ms |
508 KB |
Output is correct |
8 |
Correct |
3 ms |
544 KB |
Output is correct |
9 |
Correct |
3 ms |
544 KB |
Output is correct |
10 |
Correct |
4 ms |
544 KB |
Output is correct |
11 |
Correct |
3 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
3 ms |
628 KB |
Output is correct |
16 |
Correct |
2 ms |
628 KB |
Output is correct |
17 |
Correct |
3 ms |
636 KB |
Output is correct |
18 |
Correct |
3 ms |
636 KB |
Output is correct |
19 |
Correct |
2 ms |
636 KB |
Output is correct |
20 |
Correct |
3 ms |
636 KB |
Output is correct |
21 |
Correct |
2 ms |
636 KB |
Output is correct |
22 |
Incorrect |
2 ms |
636 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '788' |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
636 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
508 KB |
Output is correct |
5 |
Correct |
4 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
508 KB |
Output is correct |
7 |
Correct |
2 ms |
508 KB |
Output is correct |
8 |
Correct |
3 ms |
544 KB |
Output is correct |
9 |
Correct |
3 ms |
544 KB |
Output is correct |
10 |
Correct |
4 ms |
544 KB |
Output is correct |
11 |
Correct |
3 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
3 ms |
628 KB |
Output is correct |
16 |
Correct |
2 ms |
628 KB |
Output is correct |
17 |
Correct |
3 ms |
636 KB |
Output is correct |
18 |
Correct |
3 ms |
636 KB |
Output is correct |
19 |
Correct |
2 ms |
636 KB |
Output is correct |
20 |
Correct |
3 ms |
636 KB |
Output is correct |
21 |
Correct |
2 ms |
636 KB |
Output is correct |
22 |
Incorrect |
2 ms |
636 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '788' |
23 |
Halted |
0 ms |
0 KB |
- |