# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
782277 | APROHACK | Ancient Books (IOI17_books) | C++14 | 220 ms | 21048 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>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;
int nn;
ll bit[1000006]; // prefix sum hacia adelante.
void update(int pos, int val){
for(int i = pos ; i <= nn ; i += i&(-i)){
bit[i] += val;
}
}
ll query(int donde){
ll ans = 0;
for(int i = donde ; i > 0 ; i -= i&(-i)){
ans += bit[i];
}
return ans;
}
bool vis[1000006];
vector<int>perm;
ll minimo = 0;
void go(int i, int j){
if(i < j){
update(i, 1);
update(j, -1);
}else if( j < i ){
update(j, 1);
update(i, -1);
}
minimo += abs(i - j);
}
void run(int u){
if(vis[u])return ;
vis[u] = true;
go(u, perm[u]);
run(perm[u]);
}
long long minimum_walk(std::vector<int> p, int s) {
nn = p.size();
perm.pb(0);
for(int i = 0 ; i < p.size() ; i ++){
//cout << s << endl;
perm.pb(p[i]+1);
//cout << perm.back() << " ";
}
//cout << endl;
for(int i = 0 ; i <= nn ; i ++){
vis[i] = false;
bit[i] = 0;
}
for(int i = 1 ; i < nn ; i ++){
if(!vis[i])run(i);
}
ll mas = 0, cur = 0;
for(int i = 1 ; i < nn ; i ++){
if(query(i) == 0){
cur += 2;
}else{
mas += cur;
cur = 0;
}
}
//cout << minimo << " " << mas << endl;
return minimo + mas;
}
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... |