This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
bool used[N];
int a[N];
vector<int> v;
void dfs(int u){
v.push_back(u);
used[u] = true;
if(!used[a[u]]){
dfs(a[u]);
}
}
struct point{
int x, y, idx;
point();
point(int x, int y, int idx){
this->x = x;
this->y = y;
this->idx = idx;
}
};
bool between(int x1, int y1, int x2, int y2){
if(x1 > y1){
swap(x1, y1);
}
if(x2 > y2){
swap(x2, y2);
}
if(x2 < x1 && y1 < y2){
return true;
}
return false;
}
vector<point> two1, two2;
int t[N];
bool poss[N];
long long minimum_walk(vector<int> p, int s){
int n = (int)p.size();
long long ans = 0;
for(int i = 0; i < n; i++){
used[i] = false;
a[i] = p[i];
}
for(int i = 0; i < n; i++){
if(!used[i]){
v.clear();
dfs(i);
poss[i] = true;
int mn = n, mx = 0;
for(int j = 0; j < (int)v.size() - 1; j++){
ans += abs(v[j] - v[j + 1]);
//two2.push_back(point(v[j], v[j + 1], -1));
}
if(v.size() != 1){
ans += abs(v[0] - v.back());
//two2.push_back(point(v[0], v.back(), -1));
}
for(int x: v){
mn = min(mn, x);
mx = max(mx, x);
}
if(v.size() != 1){
two1.push_back(point(mn, mx, i));
}
}
}
int last = 0;
for(point p: two1){
if(p.y >= two1.back().x){
last = p.idx;
break;
}
}
ans += 2 * last;
/*if(ans == 2782){
return last;
}*/
return ans;
}
/*int main(){
int n;
cin >> n;
vector<int> p;
for(int i = 0; i < n; i++){
int x;
cin >> x;
p.push_back(x);
}
cout << minimum_walk(p, 0) << "\n";
return 0;
}*/
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |