# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
59658 | TadijaSebez | Ancient Books (IOI17_books) | C++11 | 330 ms | 243252 KiB |
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 <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
queue<int> q1,q2;
const int N=1000050;
int max(int a, int b){ return a>b?a:b;}
int min(int a, int b){ return a>b?b:a;}
int abs(int x){ return x>0?x:-x;}
ll minimum_walk(vector<int> p, int s)
{
ll sol=0;
int n=p.size(),i,j,ml=n,mr=-1;
for(i=0;i<n;i++)
{
sol+=abs(p[i]-i);
if(p[i]!=i) ml=min(ml,i),mr=max(mr,i);
}
q1.push(s);
int myl=s,myr=s;
while(1)
{
while(q1.size())
{
int x=q1.front();q1.pop();
while(myl>p[x]) q1.push(--myl);
while(myr<p[x]) q1.push(++myr);
}
int nl=myl,nr=myr,addl=0,addr=0;
bool lf=0,rf=0;
while(1)
{
if(nl<=ml) break;
nl--;addl+=2;q2.push(nl);
while(q2.size())
{
int x=q2.front();q2.pop();
if(p[x]>s) lf=1;
while(nl>p[x]) q2.push(--nl);
}
if(lf) break;
}
while(1)
{
if(nr>=mr) break;
nr++;addr+=2;q2.push(nr);
while(q2.size())
{
int x=q2.front();q2.pop();
if(p[x]<s) rf=1;
while(nr<p[x]) q2.push(++nr);
}
if(rf) break;
}
if(lf && rf) sol+=min(addl,addr);
else{ sol+=addl+addr;break;}
while(myl>nl) q1.push(--myl);
while(myr<nr) q1.push(++myr);
}
return sol;
}
Compilation message (stderr)
# | 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... |