Submission #1037035

#TimeUsernameProblemLanguageResultExecution timeMemory
1037035ALeonidouAncient Books (IOI17_books)C++17
50 / 100
101 ms30692 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" #define sz(x) (ll)x.size() #define F first #define S second #define pb push_back #define INF 1000000000000000000 typedef vector <ll> vi; typedef pair <ll,ll> ii; typedef vector <ii> vii; #define dbg(x) cout<<#x<<": "<<x<<endl; #define dbg2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<endl; #define dbg3(x,y,z) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<" "<<#z<<": "<<z<<endl; void printVct(vi &v){ for (ll i =0; i<sz(v); i++){ cout<<v[i]<<" "; } cout<<endl; } ll n; vi p; vi vis; void dfs(ll u, ll &r){ vis[u] =1; r = max(r,u); ll c = p[u]; if (!vis[c]) dfs(c,r); } long long minimum_walk(vector<int> P, int ss) { n = sz(P); p.resize(n); for (ll i =0; i<n; i++){ p[i] = P[i]; } ll s = ss; ll d = 0; for (ll i =0; i<n; i++){ d += abs(i - p[i]); } ll b = n-1; while (b >= 0 && p[b] == b) b--; ll r = 0, e = 0; vis.assign(n, 0); dfs(0, r); for (ll i =0; i<b; i++){ if (r < i) e++; if (!vis[i]) dfs(i, r); } ll ans = d + e * 2; return ans; } /* 4 0 0 2 3 1 4 0 0 2 3 1 4 0 3 2 1 0 */

Compilation message (stderr)

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:45:5: warning: unused variable 's' [-Wunused-variable]
   45 |  ll s = ss;
      |     ^
#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...