# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
118293 | tinjyu | Ancient Books (IOI17_books) | C++14 | 2 ms | 384 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 <bits/stdc++.h>
using namespace std;
bool tag[1000005];
int a[1000005];
long long int ans;
int find(long long int x)
{
ans+=abs(x-a[x]);
//cout<<x<<" "<<ans<<" "<<tag[x]<<endl;
if(tag[x]==1)return 0;
tag[x]=1;
if(x>a[x])
{
for(int i=a[x];i<=x;i++)
{
if(tag[i]==0)
{
tag[i]=1;
find(a[i]);
}
}
}
else
{
for(int i=x;i<=a[x];i++)
{
if(tag[i]==0)
{
tag[i]=1;
find(a[i]);
}
}
}
find(a[x]);
}
long long minimum_walk(std::vector<int> p, int s) {
int n=p.size();
for(int i=0;i<n;i++)a[i]=p[i];
int t=0;
for(int i=0;i<n;i++)
{
if(tag[i]==0)
{
//cout<<i<<" ";
tag[i]=1;
find(a[i]);
if(a[i]!=i)t=max(t,i);
//cout<<ans<<endl;
}
}
ans+=t+t;
return ans;
}
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... |