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 "books.h"
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef pair<long long, long long> pll;
typedef pair<char, int> ci;
typedef pair<string, int> si;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
#define pb push_back
#define fi first
#define se second
#define whole(v) v.begin(), v.end()
#define rwhole(v) v.rbegin(), v.rend()
#define inf INT_MAX/2
#define fro front
long long minimum_walk(std::vector<int> p, int s) {
vector<ll> x;
vector<ll> y;
for(int i = 0; i < p.size(); ++i){
if(p[i] == i){
x.pb(-1);
y.pb(-inf);
}else{
x.pb(i);
y.pb(-i);
}
}
sort(whole(x));
sort(whole(y));
long long ans = 0;
int i = s;
while(x[x.size()-1] != -1){
//cout << i <<" " << ans << endl;
ll k = lower_bound(x.begin(), x.end(), i) - x.begin();
if(x[k] != i){
ll j = lower_bound(y.begin(), y.end(), -i) - y.begin();
if(j == y.size()){
ans += abs(x[k] - i);
i = x[k];
continue;
}
if(k == x.size() || abs(y[j] * -1 - i) < abs(x[k] - i)){
ans += abs(y[j] * -1 - i);
i = y[j] * -1;
}else{
ans += abs(x[k] - i);
i = x[k];
}
}
ans += abs(p[i] - i);
x[lower_bound(x.begin(), x.end(), i) - x.begin()] = -1;
y[lower_bound(y.begin(), y.end(), -i) - y.begin()] = -inf;
i = p[i];
sort(whole(y));
sort(whole(x));
if(p[i] < i && upper_bound(x.begin(), x.end(), i) != x.end()){
ans += abs(i - x[upper_bound(x.begin(), x.end(), i) - x.begin()]);
i = x[upper_bound(x.begin(), x.end(), i)-x.begin()];
}
}
ans += abs(i - s);
return ans;
}
Compilation message (stderr)
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:27:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i = 0; i < p.size(); ++i){
| ~~^~~~~~~~~~
books.cpp:45:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | if(j == y.size()){
| ~~^~~~~~~~~~~
books.cpp:50:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | if(k == x.size() || abs(y[j] * -1 - i) < abs(x[k] - i)){
| ~~^~~~~~~~~~~
# | 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... |