Submission #114606

# Submission time Handle Problem Language Result Execution time Memory
114606 2019-06-02T04:23:42 Z FutymyClone Money (IZhO17_money) C++14
0 / 100
2 ms 384 KB
#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

money.cpp: In function 'int main()':
money.cpp:21:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
money.cpp:22:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
                                  ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Incorrect 2 ms 376 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Incorrect 2 ms 376 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Incorrect 2 ms 376 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Incorrect 2 ms 376 KB Output isn't correct
4 Halted 0 ms 0 KB -