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>
#ifdef GUDEB
#define D(x) cerr << #x << ": " << (x) << '\n';
#define ifdeb if(true)
#else
#define D(x) ;
#define ifdeb if(false)
#endif
#define all(x) begin(x), end(x)
using namespace std;
using ull = unsigned long long;
using ll = long long;
using pii = pair<int, int>;
// #define int ll;
struct N {
int c;
int i;
friend bool operator < (const N& a, const N& b) {
return a.c > b.c;
}
};
int n;
ll ret = 0;
priority_queue<N> q;
bool vis[1000000];
long long minimum_walk(vector<int> p, int s) {
n = p.size();
for(int i = 0; i < n; ++i) {
if(p[i] == i) vis[i] = true;
ret += abs(i - p[i]);
}
vis[s] = false;
q.push({0, s});
while(!q.empty()) {
N t = q.top();
q.pop();
D(t.i)
D(t.c)
if(vis[t.i]) continue;
ret += 2*t.c;
D(ret)
vis[t.i] = true;
q.push({0, p[t.i]});
{
int ln = t.i+1;
if(ln < n && ln == p[ln]) ++ln;
if(ln != n) {
q.push({ln - t.i, ln});
}
}
{
int rn = t.i-1;
if(rn >= 0 && rn == p[rn]) --rn;
if(rn >= 0) {
q.push({t.i - rn, rn});
}
}
}
return ret;
}
# | 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... |