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;
set<int> unvis;
int 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;
continue;
}
ret += abs(i - p[i]);
unvis.insert(i);
}
unvis.insert(s);
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;
vis[t.i] = true;
unvis.erase(t.i);
ret += 2*t.c;
D(ret)
if(t.i != p[t.i]) {
int mn = min(t.i, p[t.i]);
int mx = max(t.i, p[t.i]);
auto l = unvis.lower_bound(mn);
while(l != unvis.end() && *l <= mx) {
D(*l);
q.push({0, *l});
l = unvis.erase(l);
}
}
{
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... |