Submission #1029045

#TimeUsernameProblemLanguageResultExecution timeMemory
1029045AmirAli_H1Ancient Books (IOI17_books)C++17
22 / 100
2074 ms18816 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]; int get_min(int l, int r) { int mn = oo; for (int i = l; i < r; i++) mn = min(mn, A[i]); return mn; } int get_max(int l, int r) { int mx = -oo; for (int i = l; i < r; i++) mx = max(mx, A[i]); return mx; } 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); } } int l = s, r = s; while (l > m1 || r < m2) { int lx = min(l, get_min(l, r + 1)), rx = max(r, get_max(l, r + 1)); if (l == lx && r == rx) { res += 2; if (r < m2) r++; else l--; } else { l = lx; r = rx; } } return res; }
#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...