This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef ALEXPC
#include "books.h"
#endif
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,n) for (int i=0;i<(n);++i)
#define FORT(i,a,b) for (int i=(a);i<=(b);++i)
#define FORD(i,n) for (int i=(n)-1;i>=0;--i)
#define FORTD(i,a,b) for (int i=(a);i>=(b);--i)
#define trav(to,v) for (auto to : v)
#define all(v) v.begin(),v.end()
#define fr first
#define sc second
typedef long long ll;
const int N = 1e6+6;
long long minimum_walk(vector<int> p, int s) {
ll ans=0;
bool bl=false;
vector<pair<int,int> > vc;
FOR(i,p.size()){
ans+=llabs(p[i]-i);
if (p[i]!=i || i==s)bl=true;
if (bl)vc.push_back({min(i,p[i]),max(i,p[i])});
}
while(vc.size() && vc.back().fr==vc.back().sc && vc.back().fr!=s)vc.pop_back();
sort(all(vc));
int mx=-1;
FOR(i,vc.size()){
if (i && vc[i].fr>mx){
ans+=2;
}
if (vc[i].sc>mx)mx=vc[i].sc;
}
return ans;
}
#ifdef ALEXPC
#include <cstdio>
#include <vector>
#include <cassert>
using namespace std;
int main() {
int n, s;
assert(2 == scanf("%d %d", &n, &s));
vector<int> p((unsigned) n);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &p[(unsigned) i]));
long long res = minimum_walk(p, s);
printf("%lld\n", res);
return 0;
}
#endif
Compilation message (stderr)
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:8:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define FOR(i,n) for (int i=0;i<(n);++i)
| ^
books.cpp:26:2: note: in expansion of macro 'FOR'
26 | FOR(i,p.size()){
| ^~~
books.cpp:8:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define FOR(i,n) for (int i=0;i<(n);++i)
| ^
books.cpp:34:2: note: in expansion of macro 'FOR'
34 | FOR(i,vc.size()){
| ^~~
# | 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... |