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>
#define pb push_back
#include "books.h"
#define ll long long
using namespace std;
//#define DEBUGGING
#ifdef DEBUGGING
#include "../debug.h"
#else
#define debug(x...) void(42)
#endif
struct Node
{
int head, tail, size, nxt;
Node(int i) : head(i), tail(i), size(1), nxt(-1) {}
Node() : Node(-1) {}
};
constexpr static int MXN = 1e6;
Node node[MXN];
bitset<MXN> visited;
bitset<MXN> visited_component;
void merge(int a, int b);
ll minimum_walk(vector<int> p, int s)
{
int n = p.size();
ll sum = 0;
int current = 0;
vector<int> rp(n);
for (int i = 0; i < n; i++)
rp[p[i]] = i;
vector<int> pp;
int spos = MXN;
int epos = -1;
vector<int> v;
debug(p, rp);
vector<int> possibilities;
for (int i = 0; i < n-1; i++)
{
if (p[i] > i)
current++;
if (rp[i] < i)
current--;
if (p[i] != i)
possibilities.pb(i);
if (current > 0)
{
spos = min(spos, i);
epos = max(epos, i+1);
}
debug(current, i);
sum += current * 2;
v.pb(current);
}
if (sum == 0)
return sum;
for (int i = spos; i < epos; i++)
{
if (v[i] == 0)
sum += 2;
}
debug(spos, epos, sum);
int a = upper_bound(possibilities.begin(), possibilities.end(), s) - possibilities.begin();
int first_cost = MXN;
if (a < n)
first_cost = min(first_cost, possibilities[a] - s);
a--;
if (a >= 0)
first_cost = min(first_cost, s - possibilities[a]);
return sum + first_cost * 2;
}
void merge(int a, int b)
{
if (node[a].size < node[b].size) swap(a, b);
node[a].size += node[b].size;
node[node[a].tail].nxt = b;
node[a].tail = node[b].tail;
for (;b!=-1;b=node[b].nxt)
node[b].head = a;
}
# | 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... |