Submission #423454

#TimeUsernameProblemLanguageResultExecution timeMemory
423454Theo830Ancient Books (IOI17_books)C++17
0 / 100
1 ms460 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1e9; ll MOD = 998244353; typedef pair<ll,ll> ii; #define iii pair<int,int> #define f(i,a,b) for(ll i = a;i < b;i++) #define pb push_back #define vll vector<ll> #define F first #define S second #define all(x) (x).begin(), (x).end() ///I hope I will get uprating and don't make mistakes ///I will never stop programming ///sqrt(-1) Love C++ ///Please don't hack me ///@TheofanisOrfanou Theo830 ///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst) ///Stay Calm ///Look for special cases ///Beware of overflow and array bounds ///Think the problem backwards ///Training #include "books.h" long long minimum_walk(std::vector<int> p, int s) { ll ans = 0; ll n = p.size(); assert(s == 0 && n <= 1000); bool v[n] = {0}; ll dist = 0; f(i,0,n){ if(p[i] != i && !v[i]){ dist = i; ll last = i; ll st = i; ll pos = p[i]; v[i] = 1; while(pos != st){ v[pos] = 1; ans += abs(pos - last); last = pos; pos = p[pos]; } ans += abs(pos - last); } else{ v[i] = 1; } } ans += 2 * dist; return ans; } /* using namespace std; int main() { int n, s; assert(2 == scanf("%d %d", &n, &s)); vector<int> p((unsigned) n); for(int i = 0; i < n; i++) assert(1 == scanf("%d", &p[(unsigned) i])); long long res = minimum_walk(p, s); printf("%lld\n", res); return 0; } */ /* 4 0 0 2 3 1 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...