Submission #1029092

#TimeUsernameProblemLanguageResultExecution timeMemory
1029092AmirAli_H1Ancient Books (IOI17_books)C++17
100 / 100
164 ms32852 KiB
// In the name of Allah #include <bits/stdc++.h> #include "books.h" using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef complex<ld> cld; #define all(x) (x).begin(),(x).end() #define len(x) ((ll) (x).size()) #define F first #define S second #define pb push_back #define sep ' ' #define endl '\n' #define Mp make_pair #define kill(x) cout << x << '\n', exit(0) #define set_dec(x) cout << fixed << setprecision(x); #define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int maxn = 1e6 + 4; const int oo = 1e9 + 7; int n; ll res; int A[maxn], mark[maxn]; int sm[maxn], mn, mx; int dis[maxn]; deque<int> dq; void dfs(int v) { mark[v] = 1; mn = min(mn, v); mx = max(mx, v); if (!mark[A[v]]) dfs(A[v]); } void bfs01(int s) { fill(mark, mark + n, 0); fill(dis, dis + n, oo); dis[s] = 0; dq.pb(s); while (!dq.empty()) { int v = dq.front(); dq.pop_front(); if (mark[v]) continue; mark[v] = 1; if (v - 1 >= 0) { int u = v - 1, w = (abs(v - s) < abs(u - s)); if (dis[v] + w < dis[u]) { dis[u] = dis[v] + w; if (w == 0) dq.push_front(u); else dq.pb(u); } } if (v + 1 < n) { int u = v + 1, w = (abs(v - s) < abs(u - s)); if (dis[v] + w < dis[u]) { dis[u] = dis[v] + w; if (w == 0) dq.push_front(u); else dq.pb(u); } } int u = A[v], w = 0; if (dis[v] + w < dis[u]) { dis[u] = dis[v] + w; if (w == 0) dq.push_front(u); else dq.pb(u); } } } ll minimum_walk(vector<int> p, int s) { n = len(p); res = 0; for (int i = 0; i < n; i++) { A[i] = p[i]; res += abs(i - A[i]); } int m1 = s, m2 = s; for (int i = 0; i < n; i++) { if (A[i] != i) { m1 = min(m1, i); m2 = max(m2, i); } if (!mark[i]) { mn = i; mx = i; dfs(i); sm[mn]++; sm[mx]--; } } for (int i = 1; i < n; i++) { sm[i] += sm[i - 1]; } int l = s, r = s; for (int i = s; i < m2; i++) { if (sm[i] != 0) r = i + 1; else break; } for (int i = s - 1; i >= m1; i--) { if (sm[i] != 0) l = i; else break; } for (int i = m1; i < m2; i++) { if (sm[i] == 0) res += 2; } bfs01(s); res += 2 * dis[l]; return res; }

Compilation message (stderr)

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:95:13: warning: variable 'r' set but not used [-Wunused-but-set-variable]
   95 |  int l = s, r = s;
      |             ^
#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...