Submission #1250661

#TimeUsernameProblemLanguageResultExecution timeMemory
1250661quangminh412Financial Report (JOI21_financial)C++20
100 / 100
587 ms38152 KiB
/*
  Ben Watson
  Handle codeforces : quangminh98

  Current Theme: Transformers !!!!
*/

#include <bits/stdc++.h>
using namespace std;

#define ll long long

const string name = "test";

void solve();
signed main()
{
    if (fopen((name + ".inp").c_str(), "r"))
    {
        freopen((name + ".inp").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    solve();

    return 0;
}

// main program
const int oo = 0x3f3f3f3f;
const int maxn = 3e5 + 1;

struct MinSegmentTree
{
    struct Node
    {
        int mn, lazy;

        Node(int val) : mn(val), lazy(oo) {};
        Node operator + (const Node& other)
        {
            Node res = Node(oo);
            res.mn = min(mn, other.mn);
            return res;
        }
    };
    vector<Node> st;
    int n;

    MinSegmentTree(int n) : n(n) { st.resize(4 * n + 1, Node(oo)); }

    void pass(int head, int l, int r)
    {
        if (st[head].lazy == oo)
            return;

        st[head].mn = min(st[head].mn, st[head].lazy);
        if (l != r)
        {
            st[head << 1].lazy = min(st[head << 1].lazy, st[head].lazy);
            st[head << 1 | 1].lazy = min(st[head << 1 | 1].lazy, st[head].lazy);
        }

        st[head].lazy = oo;
    }

    void update(int head, int l, int r, int u, int v, int val)
    {
        pass(head, l, r);
        if (l > v || r < u) return;
        if (u <= l && r <= v)
        {
            st[head].lazy = min(st[head].lazy, val);
            pass(head, l, r);
            return;
        }

        int mid = l + r >> 1;
        update(head << 1, l, mid, u, v, val);
        update(head << 1 | 1, mid + 1, r, u, v, val);
        st[head] = st[head << 1] + st[head << 1 | 1];
    }
    void update(int u, int v, int val) { if (u > v) return; update(1, 1, n, u, v, val); }

    int query(int head, int l, int r, int pos)
    {
        pass(head, l, r);
        if (pos < l || r < pos) return oo;
        if (l == r) return st[head].mn;

        int mid = l + r >> 1;
        if (pos <= mid)
            return query(head << 1, l, mid, pos);
        return query(head << 1 | 1, mid + 1, r, pos);
    }
    int query(int pos) { return query(1, 1, n, pos); }
};

struct MaxSegmentTree
{
    vector<int> st;
    int n;

    MaxSegmentTree(int n) : n(n) { st.resize(4 * n + 1, 0); }

    void update(int head, int l, int r, int pos, int val)
    {
        if (pos < l || r < pos) return;
        if (l == r)
        {
            st[head] = val;
            return;
        }

        int mid = l + r >> 1;
        if (pos <= mid)
            update(head << 1, l, mid, pos, val);
        else
            update(head << 1 | 1, mid + 1, r, pos, val);
        st[head] = max(st[head << 1], st[head << 1 | 1]);
    }
    void update(int pos, int val) { update(1, 1, n, pos, val); }

    int query(int head, int l, int r, int u, int v)
    {
        if (l > v || r < u) return 0;
        if (u <= l && r <= v) return st[head];

        int mid = l + r >> 1;
        return max(query(head << 1, l, mid, u, v), query(head << 1 | 1, mid + 1, r, u, v));
    }
    int query(int u, int v) { return query(1, 1, n, u, v); }
};

int n, d, cur;
map<int, int> cmp;
int a[maxn], dp[maxn], R[maxn];

void solve()
{
    cin >> n >> d;
    cur = 0;
    vector<int> v;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        v.push_back(a[i]);
    }

    // coordinate compression
    sort(v.begin(), v.end());
    cur = 0;
    for (int x : v)
        if (cmp[x] == 0)
            cmp[x] = ++cur;

    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    MinSegmentTree st_min(cur);
    for (int i = n; i > 0; i--)
    {
        R[i] = n;
        pq.push({a[i], i});
        while (!pq.empty() && pq.top().second >= i + d)
            pq.pop();

        if (i + d <= n)
            R[i] = min(R[i], st_min.query(cmp[a[i]]));

        if (i + d - 1 <= n)
            st_min.update(1, cmp[pq.top().first] - 1, i + d - 1);
    }

    vector<pair<int, int>> proc;
    for (int i = 1; i <= n; i++)
        proc.push_back({a[i], i});
    sort(proc.begin(), proc.end(), greater<pair<int, int>>());

    MaxSegmentTree st_max(n);
    int res = 0, i = 0;
    while (i < n)
    {
        int val = proc[i].first;
        vector<int> save;
        while (i < n && proc[i].first == val)
        {
            int pos = proc[i].second;
            dp[pos] = st_max.query(pos, R[pos]) + 1;
            res = max(res, dp[pos]);
            save.push_back(pos);
            i++;
        }
        for (int idx : save)
            st_max.update(idx, dp[idx]);
    }

    cout << res << '\n';
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen((name + ".inp").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...