Submission #886493

# Submission time Handle Problem Language Result Execution time Memory
886493 2023-12-12T08:35:06 Z boris_mihov Money (IZhO17_money) C++17
100 / 100
463 ms 23004 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <bitset>
#include <vector>
#include <stack>

typedef long long llong;
const int MAXN = 1000000 + 10;
const int MAXLOG = 20;

int n;
struct Fenwick
{
    int tree[MAXN];
    void update(int pos, int val)
    {
        for (int idx = pos ; idx < MAXN ; idx += idx & (-idx))
        {
            tree[idx] += val;
        }
    }

    int query(int pos)
    {
        int res = 0;
        for (int idx = pos ; idx > 0 ; idx -= idx & (-idx))
        {
            res += tree[idx];
        }

        return res;
    }

    int findKth(int k)
    {
        int pos = 0;
        for (int log = MAXLOG - 1 ; log >= 0 ; --log)
        {
            if (pos + (1 << log) < MAXN && tree[pos + (1 << log)] < k)
            {
                pos += (1 << log);
                k -= tree[pos];
            }
        }

        return pos + 1;
    }
};

int a[MAXN];
int dp[MAXN];
int prefix[MAXN];
bool endHere[MAXN];
std::stack <int> st;
std::deque <std::pair <int,int>> dq;
Fenwick tree;

void solve()
{
    for (int i = 1 ; i <= n ; ++i)
    {
        prefix[i] = prefix[i - 1] + (a[i - 1] > a[i]);
        tree.update(a[i], 1);
    }

    dq.push_back({n + 1, 0});
    for (int i = n ; i >= 1 ; --i)
    {
        tree.update(a[i], -1);
        int allowedTo = tree.findKth(tree.query(a[i]) + 1);
        int l = i, r = n + 1, mid;
        while (l < r - 1)
        {
            mid = (l + r) / 2;
            if (a[mid] <= allowedTo && prefix[mid] - prefix[i] == 0)
            {
                l = mid;
            } else
            {
                r = mid;
            }
        }

        while (dq.size() && dq.front().first > l + 1)
        {
            dq.pop_front();
        }

        assert(dq.size());
        dp[i] = dq.front().second + 1;
        while (dq.size() && dq.back().second >= dp[i])
        {
            dq.pop_back();
        }

        dq.push_back({i, dp[i]});
    }

    std::cout << dp[1] << '\n';
}

void input()
{
    std::cin >> n;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> a[i];
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}

/*
8
7 6 8 1 5 2 4 3
7 | 5 7 | 1 4 | 1 2 | 1

1 | 1 |1 2| 4 | 5 7 | 7

7 5 7 1 4 1 2 1

5
7 | 5 7 | 1 5 2 4 3
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8660 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8548 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8672 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8668 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8660 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8548 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8672 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8668 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
17 Correct 1 ms 8536 KB Output is correct
18 Correct 1 ms 8664 KB Output is correct
19 Correct 1 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 1 ms 8668 KB Output is correct
22 Correct 1 ms 8540 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 1 ms 8540 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8660 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8548 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8672 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8668 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
17 Correct 1 ms 8536 KB Output is correct
18 Correct 1 ms 8664 KB Output is correct
19 Correct 1 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 1 ms 8668 KB Output is correct
22 Correct 1 ms 8540 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 1 ms 8540 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8540 KB Output is correct
29 Correct 1 ms 8540 KB Output is correct
30 Correct 1 ms 8540 KB Output is correct
31 Correct 1 ms 8540 KB Output is correct
32 Correct 1 ms 8544 KB Output is correct
33 Correct 1 ms 8540 KB Output is correct
34 Correct 1 ms 8540 KB Output is correct
35 Correct 1 ms 8540 KB Output is correct
36 Correct 1 ms 8540 KB Output is correct
37 Correct 1 ms 8540 KB Output is correct
38 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8660 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8548 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8672 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8668 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
17 Correct 1 ms 8536 KB Output is correct
18 Correct 1 ms 8664 KB Output is correct
19 Correct 1 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 1 ms 8668 KB Output is correct
22 Correct 1 ms 8540 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 1 ms 8540 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8540 KB Output is correct
29 Correct 1 ms 8540 KB Output is correct
30 Correct 1 ms 8540 KB Output is correct
31 Correct 1 ms 8540 KB Output is correct
32 Correct 1 ms 8544 KB Output is correct
33 Correct 1 ms 8540 KB Output is correct
34 Correct 1 ms 8540 KB Output is correct
35 Correct 1 ms 8540 KB Output is correct
36 Correct 1 ms 8540 KB Output is correct
37 Correct 1 ms 8540 KB Output is correct
38 Correct 1 ms 8540 KB Output is correct
39 Correct 143 ms 19412 KB Output is correct
40 Correct 260 ms 21380 KB Output is correct
41 Correct 121 ms 14672 KB Output is correct
42 Correct 118 ms 12124 KB Output is correct
43 Correct 93 ms 11272 KB Output is correct
44 Correct 317 ms 22704 KB Output is correct
45 Correct 352 ms 22688 KB Output is correct
46 Correct 346 ms 22456 KB Output is correct
47 Correct 262 ms 22608 KB Output is correct
48 Correct 321 ms 22692 KB Output is correct
49 Correct 395 ms 22864 KB Output is correct
50 Correct 357 ms 22872 KB Output is correct
51 Correct 423 ms 22872 KB Output is correct
52 Correct 393 ms 22728 KB Output is correct
53 Correct 398 ms 22868 KB Output is correct
54 Correct 387 ms 22612 KB Output is correct
55 Correct 450 ms 22868 KB Output is correct
56 Correct 437 ms 22864 KB Output is correct
57 Correct 463 ms 22868 KB Output is correct
58 Correct 463 ms 22728 KB Output is correct
59 Correct 462 ms 22880 KB Output is correct
60 Correct 450 ms 22864 KB Output is correct
61 Correct 438 ms 22868 KB Output is correct
62 Correct 452 ms 23004 KB Output is correct