# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
114606 | FutymyClone | Money (IZhO17_money) | C++14 | 2 ms | 384 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, a[N], nxt[N], bit[N];
stack <int> st;
void update (int x, int val) {
for (int i = x; i < N; i += i & -i) bit[i] += val;
}
int query (int x) {
int ans = 0;
for (int i = x; i > 0; i -= i & -i) ans += bit[i];
return ans;
}
int main(){
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
if (st.empty() || a[st.top()] <= a[i]) st.push(i);
else {
while (!st.empty()) nxt[st.top()] = i, st.pop();
st.push(i);
}
}
while (!st.empty()) nxt[st.top()] = n + 1, st.pop();
int cur = 1, ans = 0;
while (cur <= n) {
int l = cur, r = nxt[cur] - 1;
while (l <= r) {
int mid = (l + r) / 2;
int res = query(a[mid]) - query(a[cur] - 1);
if (res == 0 || res == a[mid] - a[cur] + 1) l = mid + 1;
else r = mid - 1;
}
ans++;
update(a[cur], 1); update(a[r] + 1, -1);
cur = l;
}
printf("%d", ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |