#include"books.h"
#include <vector>
#include<bits/stdc++.h>
const int maxn=1000000+10;
using namespace std;
int vis[maxn],visa[maxn],av[maxn];
int n;
long long mainres=0,last=0;
vector<pair<int,pair<int,int>>>alle;
struct dsu{
int par[maxn];
dsu(){
for(int i=0;i<maxn;i++){
par[i]=i;
}
}
int root(int u){
if(u==par[u]){
return u;
}
return par[u]=root(par[u]);
}
int con(int u,int v){
u=root(u);
v=root(v);
if(u==v){
return 0;
}
par[u]=v;
return 1;
}
}ds;
void mst(){
sort(alle.begin(),alle.end());
for(int i=0;i<(int)alle.size();i++){
if(ds.con(alle[i].second.first,alle[i].second.second)){
mainres+=alle[i].first;
}
}
}
long long minimum_walk(std::vector<int> p, int s){
mainres=0;
n=p.size();
for(int i=0;i<=n;i++){
vis[i]=visa[i]=0;
}
last=0;
int cnt1=0;
for(int i=0;i<n;i++){
if(p[i]>i){
cnt1++;
visa[p[i]]=1;
}
if(visa[i]){
cnt1--;
}
mainres+=cnt1*2;
if(cnt1!=0){
last=i+1;
}
}
for(int i=0;i<=last;i++){
if(i==p[i]){
continue;
}
if(vis[i]==0){
//mainres+=2;
int now=i;
do{
av[now]=i;
vis[now]=1;
now=p[now];
}while(now!=i);
}
}
last=-1;
alle.clear();
for(int i=0;i<n;i++){
if(i==s){
last=i;
}
if(vis[i]){
if(last!=-1){
alle.push_back(make_pair((i-last)*2,make_pair(av[i],av[last])));
}
last=i;
}
}
mst();
return mainres;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4184 KB |
Output is correct |
6 |
Incorrect |
2 ms |
4188 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4184 KB |
Output is correct |
6 |
Incorrect |
2 ms |
4188 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4184 KB |
Output is correct |
6 |
Incorrect |
2 ms |
4188 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4184 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4074' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4184 KB |
Output is correct |
6 |
Incorrect |
2 ms |
4188 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |