#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];
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;
}
}
}
}
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:47:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<e.size();i++){
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
540 KB |
Output is correct |
4 |
Correct |
3 ms |
540 KB |
Output is correct |
5 |
Correct |
3 ms |
588 KB |
Output is correct |
6 |
Incorrect |
3 ms |
588 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
540 KB |
Output is correct |
4 |
Correct |
3 ms |
540 KB |
Output is correct |
5 |
Correct |
3 ms |
588 KB |
Output is correct |
6 |
Incorrect |
3 ms |
588 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
540 KB |
Output is correct |
4 |
Correct |
3 ms |
540 KB |
Output is correct |
5 |
Correct |
3 ms |
588 KB |
Output is correct |
6 |
Incorrect |
3 ms |
588 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
588 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4074' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
540 KB |
Output is correct |
4 |
Correct |
3 ms |
540 KB |
Output is correct |
5 |
Correct |
3 ms |
588 KB |
Output is correct |
6 |
Incorrect |
3 ms |
588 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |