Submission #156191

# Submission time Handle Problem Language Result Execution time Memory
156191 2019-10-04T11:08:39 Z theboatman Money (IZhO17_money) C++17
45 / 100
1500 ms 38728 KB
#include <bits/stdc++.h>

#define y1 theboatman
#define make_struct(args...) {args}

using namespace std;

const int inf = 1e9 + 10;
const int N = 1e6 + 10;

struct tree {
    vector <int> t;

    int tl, tr, v;
    void init(int n) {
        tl = 0;
        tr = n - 1;
        v = 1;

        t.resize(n * 4);
    }

    void inc(int v, int tl, int tr, int pos) {
        if (tl == tr) {
            t[v] |= 1;
        }
        else {
            int tm = tl + tr >> 1;
            if (pos <= tm) {
                inc(v << 1, tl, tm, pos);
            }
            else {
                inc(v << 1 | 1, tm + 1, tr, pos);
            }
            
            t[v] = t[v << 1] + t[v << 1 | 1];
        }

    }

    void inc(int pos) {
        inc(v, tl, tr, pos);
    }

    int get(int v, int tl, int tr, int l, int r) {
        if (l > r) {
            return 0;
        }

        if (tl == l && tr == r) {
            return t[v];
        }

        int tm = tl + tr >> 1;
        int ql = get(v << 1, tl, tm, l, min(r, tm));
        int qr = get(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r);

        return ql + qr;
    }

    int get(int l, int r) {
        return get(v, tl, tr, l, r);
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    int n;
    cin >> n;

    vector <int> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }

    int now = 0, x = 0;
    while(now < n && x <= a[now]) {
        x = a[now];
        now++;
    }

    tree bank;
    bank.init(N);

    set <int> s;
    for(int i = 0; i < now; i++) {
        bank.inc(a[i]);

        s.insert(a[i]);
    }

    int ans = 0;
    for(int i = now; i < n; i++) {
        ans++;

        int l = *s.begin(), r = *--s.end();
        //cout << a[i] << " " << l << " " << r << endl;
        if (a[i] >= r) {
            int x = 0;
            while(i < n && x <= a[i]) {
                x = a[i];

                bank.inc(x);
                s.insert(x);

                i++;
            }
            i--;
        }
        else {
            vector <int> lazy;
            if (a[i] < l) {
                int x = 0;
                while(i < n && x <= a[i] && a[i] <= l) {
                    x = a[i];
                    lazy.push_back(x);
                    i++;
                }
                i--;
            }
            else {
                int x = 0;
                int les = *--s.upper_bound(a[i]);

                while(i < n && x <= a[i]) {
                    auto more = s.lower_bound(a[i]);
                    if (more == s.end()) {
                        break;
                    }

                    int pos = bank.get(0, les);
                    int pos1 = bank.get(0, *more);

                    //cout << les << " " << *more << " " << pos << " " << pos1 << " " << i << endl;

                    if (pos1 - pos <= 1) {
                        x = a[i];
                        lazy.push_back(x);
                        i++;
                    }
                    else {
                        break;
                    }
                }

                i--;
            }

            for(auto it : lazy) {
                bank.inc(it);
                s.insert(it);
            }
        }
    }

    cout << ans + 1 << "\n";
    return 0;
}

/*
find_by_order(X) k -> элемент, нумерация с нуля
order_of_key(X) кол-во < чем X
*/

Compilation message

money.cpp: In member function 'void tree::inc(int, int, int, int)':
money.cpp:28:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
             int tm = tl + tr >> 1;
                      ~~~^~~~
money.cpp: In member function 'int tree::get(int, int, int, int, int)':
money.cpp:54:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int tm = tl + tr >> 1;
                  ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 15992 KB Output is correct
2 Correct 16 ms 15992 KB Output is correct
3 Correct 18 ms 15992 KB Output is correct
4 Correct 18 ms 15992 KB Output is correct
5 Correct 16 ms 16028 KB Output is correct
6 Correct 16 ms 15992 KB Output is correct
7 Correct 15 ms 15996 KB Output is correct
8 Correct 16 ms 15964 KB Output is correct
9 Correct 18 ms 15992 KB Output is correct
10 Correct 19 ms 15992 KB Output is correct
11 Correct 18 ms 15992 KB Output is correct
12 Correct 16 ms 16120 KB Output is correct
13 Correct 15 ms 15992 KB Output is correct
14 Correct 16 ms 15992 KB Output is correct
15 Correct 16 ms 15992 KB Output is correct
16 Correct 18 ms 15992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 15992 KB Output is correct
2 Correct 16 ms 15992 KB Output is correct
3 Correct 18 ms 15992 KB Output is correct
4 Correct 18 ms 15992 KB Output is correct
5 Correct 16 ms 16028 KB Output is correct
6 Correct 16 ms 15992 KB Output is correct
7 Correct 15 ms 15996 KB Output is correct
8 Correct 16 ms 15964 KB Output is correct
9 Correct 18 ms 15992 KB Output is correct
10 Correct 19 ms 15992 KB Output is correct
11 Correct 18 ms 15992 KB Output is correct
12 Correct 16 ms 16120 KB Output is correct
13 Correct 15 ms 15992 KB Output is correct
14 Correct 16 ms 15992 KB Output is correct
15 Correct 16 ms 15992 KB Output is correct
16 Correct 18 ms 15992 KB Output is correct
17 Correct 18 ms 16024 KB Output is correct
18 Correct 17 ms 16076 KB Output is correct
19 Correct 16 ms 15992 KB Output is correct
20 Correct 15 ms 15992 KB Output is correct
21 Correct 15 ms 15992 KB Output is correct
22 Correct 28 ms 15992 KB Output is correct
23 Correct 16 ms 15992 KB Output is correct
24 Correct 16 ms 15992 KB Output is correct
25 Correct 16 ms 15992 KB Output is correct
26 Correct 16 ms 15992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 15992 KB Output is correct
2 Correct 16 ms 15992 KB Output is correct
3 Correct 18 ms 15992 KB Output is correct
4 Correct 18 ms 15992 KB Output is correct
5 Correct 16 ms 16028 KB Output is correct
6 Correct 16 ms 15992 KB Output is correct
7 Correct 15 ms 15996 KB Output is correct
8 Correct 16 ms 15964 KB Output is correct
9 Correct 18 ms 15992 KB Output is correct
10 Correct 19 ms 15992 KB Output is correct
11 Correct 18 ms 15992 KB Output is correct
12 Correct 16 ms 16120 KB Output is correct
13 Correct 15 ms 15992 KB Output is correct
14 Correct 16 ms 15992 KB Output is correct
15 Correct 16 ms 15992 KB Output is correct
16 Correct 18 ms 15992 KB Output is correct
17 Correct 18 ms 16024 KB Output is correct
18 Correct 17 ms 16076 KB Output is correct
19 Correct 16 ms 15992 KB Output is correct
20 Correct 15 ms 15992 KB Output is correct
21 Correct 15 ms 15992 KB Output is correct
22 Correct 28 ms 15992 KB Output is correct
23 Correct 16 ms 15992 KB Output is correct
24 Correct 16 ms 15992 KB Output is correct
25 Correct 16 ms 15992 KB Output is correct
26 Correct 16 ms 15992 KB Output is correct
27 Correct 18 ms 15992 KB Output is correct
28 Correct 16 ms 15992 KB Output is correct
29 Correct 18 ms 15992 KB Output is correct
30 Correct 16 ms 15992 KB Output is correct
31 Correct 16 ms 15992 KB Output is correct
32 Correct 16 ms 15992 KB Output is correct
33 Correct 16 ms 16120 KB Output is correct
34 Correct 16 ms 15992 KB Output is correct
35 Correct 16 ms 15992 KB Output is correct
36 Correct 16 ms 15992 KB Output is correct
37 Correct 16 ms 15992 KB Output is correct
38 Correct 16 ms 15992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 15992 KB Output is correct
2 Correct 16 ms 15992 KB Output is correct
3 Correct 18 ms 15992 KB Output is correct
4 Correct 18 ms 15992 KB Output is correct
5 Correct 16 ms 16028 KB Output is correct
6 Correct 16 ms 15992 KB Output is correct
7 Correct 15 ms 15996 KB Output is correct
8 Correct 16 ms 15964 KB Output is correct
9 Correct 18 ms 15992 KB Output is correct
10 Correct 19 ms 15992 KB Output is correct
11 Correct 18 ms 15992 KB Output is correct
12 Correct 16 ms 16120 KB Output is correct
13 Correct 15 ms 15992 KB Output is correct
14 Correct 16 ms 15992 KB Output is correct
15 Correct 16 ms 15992 KB Output is correct
16 Correct 18 ms 15992 KB Output is correct
17 Correct 18 ms 16024 KB Output is correct
18 Correct 17 ms 16076 KB Output is correct
19 Correct 16 ms 15992 KB Output is correct
20 Correct 15 ms 15992 KB Output is correct
21 Correct 15 ms 15992 KB Output is correct
22 Correct 28 ms 15992 KB Output is correct
23 Correct 16 ms 15992 KB Output is correct
24 Correct 16 ms 15992 KB Output is correct
25 Correct 16 ms 15992 KB Output is correct
26 Correct 16 ms 15992 KB Output is correct
27 Correct 18 ms 15992 KB Output is correct
28 Correct 16 ms 15992 KB Output is correct
29 Correct 18 ms 15992 KB Output is correct
30 Correct 16 ms 15992 KB Output is correct
31 Correct 16 ms 15992 KB Output is correct
32 Correct 16 ms 15992 KB Output is correct
33 Correct 16 ms 16120 KB Output is correct
34 Correct 16 ms 15992 KB Output is correct
35 Correct 16 ms 15992 KB Output is correct
36 Correct 16 ms 15992 KB Output is correct
37 Correct 16 ms 15992 KB Output is correct
38 Correct 16 ms 15992 KB Output is correct
39 Correct 279 ms 18012 KB Output is correct
40 Correct 492 ms 19340 KB Output is correct
41 Correct 215 ms 17784 KB Output is correct
42 Correct 233 ms 17596 KB Output is correct
43 Correct 173 ms 17244 KB Output is correct
44 Correct 602 ms 20216 KB Output is correct
45 Correct 576 ms 20156 KB Output is correct
46 Correct 615 ms 20188 KB Output is correct
47 Correct 509 ms 20088 KB Output is correct
48 Correct 496 ms 20216 KB Output is correct
49 Execution timed out 1572 ms 38728 KB Time limit exceeded
50 Halted 0 ms 0 KB -