Submission #466233

#TimeUsernameProblemLanguageResultExecution timeMemory
466233prvocisloExercise Deadlines (CCO20_day1problem2)C++17
25 / 25
145 ms7112 KiB
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <queue> typedef long long ll; using namespace std; const int maxn = 1 << 18; int maxi[maxn * 2], alive[maxn * 2]; void merge(int i) { maxi[i] = max(maxi[i << 1], maxi[i << 1 | 1]); alive[i] = alive[i << 1] + alive[i << 1 | 1]; } void remove_element(int i) { i += maxn; maxi[i] = -1, alive[i] = 0; for (i = (i >> 1); i > 0; i >>= 1) merge(i); } int last_big(int x) { int i = 1; for (; i < maxn; ) { if (maxi[i << 1 | 1] < x) i = i << 1; else i = i << 1 | 1; } return i - maxn; } int sum(int l, int r) { int s = 0; for (l += maxn, r += maxn + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) s += alive[l++]; if (r & 1) s += alive[--r]; } return s; } int main() { ios::sync_with_stdio(false); cin.tie(0); for (int i = 0; i < maxn * 2; i++) maxi[i] = -1; int n; cin >> n; vector<int> d(n); for (int i = 0; i < n; i++) cin >> d[i], maxi[i + maxn] = --d[i], alive[i + maxn] = 1; for (int i = maxn - 1; i > 0; i--) merge(i); vector<int> v = d; sort(v.begin(), v.end()); for (int i = 0; i < n; i++) if (v[i] < i) { cout << "-1\n"; return 0; } ll ans = 0; for (int i = n - 1; i >= 0; i--) { int j = last_big(i); //cout << j << endl; ans += sum(j, n - 1) - 1; remove_element(j); } cout << ans << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...