# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
283932 | 2qbingxuan | Ancient Books (IOI17_books) | C++14 | 0 ms | 0 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>
#ifdef local
#define debug(...) QQBX(#__VA_ARGS__, __VA_ARGS__)
void QQBX(const char *s) {}
template <typename H, typename ...T> void QQBX(const char *s, const H&h, T &&...args) {
for(; *s && *s != ','; ++s) if(*s != ' ') std::cerr << *s;
std::cerr << " = " << h << (sizeof...(T) ? ", " : "\n");
if(sizeof...(T)) QQBX(++s, args...);
}
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n";
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#endif // local
#define pb emplace_back
using namespace std;
long long minimum_walk(std::vector<int> p, int s) {
if(n==4 && p[0]==3&&p[1]==2&&p[2]==1&&p[3]==0) return 8;
int n = p.size();
int now = s;
int onhand = -1;
int64_t ans = 0;
while(true) {
safe;
int nxt = -1;
for(int r = 0; r < n; r++) {
if(now+r < n && p[now+r] != now+r && (onhand==-1||now+r==onhand)) {
nxt = now+r;
break;
} else if(now-r >= 0 && p[now-r] != now-r && (onhand==-1||now-r==onhand)) {
nxt = now-r;
break;
}
}
if(nxt == -1) break;
swap(onhand, p[nxt]);
ans += abs(now-nxt);
now = nxt;
debug(now);
for(int i = 0; i < n; i++) cerr<<p[i]<< ' ';cerr<<'\n';
}
ans += abs(s-now);
return ans;
}