제출 #1205088

#제출 시각아이디문제언어결과실행 시간메모리
1205088sunflowerFinancial Report (JOI21_financial)C++17
31 / 100
83 ms5816 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define SZ(x) ((int) (x).size())
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define debug(x) cout << "[" << #x << " = " << (x) << "]" << endl

#define left    __left
#define right   __right
#define prev    __prev
#define fi      first
#define se      second

template <class X, class Y>
    bool maximize(X &x, Y y) {
        if (x < y) return x = y, true;
        else return false;
    }

template <class X, class Y>
    bool minimize(X &x, Y y) {
        if (x > y) return x = y, true;
        else return false;
    }

template <class T>
    void remove_dup(vector <T> &v) {
        sort(ALL(v));
        v.resize(unique(ALL(v)) - v.begin());
    }
    int POS(int x, const vector <int> &v) {
        return lower_bound(ALL(v), x) - v.begin() + 1;
    }

int n, maxRange;

#define MAX_N 300'300
int a[MAX_N + 7];

namespace subtask1 {
    bool check() {
        return (n <= 20);
    }

    int ids[25], cnt_ids = 0;

    void solve() {
        int ans = 0;
        FOR(mask, 1, MASK(n) - 1) {
            bool ok = true;
            cnt_ids = 0;
            FOR(i, 0, n - 1) if (BIT(mask, i)) {
                if (cnt_ids > 0) {
                    if (i + 1 - ids[cnt_ids] > maxRange) {ok = false; break;}
                }
                ids[++cnt_ids] = i + 1;
            }
            if (!ok) continue;

            int res = 1, ma = a[ids[1]];
            FOR(i, 2, cnt_ids) {
                if (a[ids[i]] > ma) {
                    ++res;
                    ma = a[ids[i]];
                }
            }

            maximize(ans, res);
        }

        cout << ans;
    }
};

namespace subtask2 {
    bool check() {
        return (n <= 400);
    }

    int dp[MAX_N + 7];

    void solve() {
        /// dp[i]: day con dai nhat ma co a_i la max;
        FOR(i, 1, n) {
            dp[i] = 1;
            FOR(j, 1, i - 1) {
                if (a[j] < a[i]) {
                    bool ok = true;

                    int lst = j;
                    FOR(k, j + 1, i - 1) {
                        if (a[k] <= a[i] && a[k] <= a[j]) {
                            if (lst - k > maxRange) {ok = false; break;}
                            lst = k;
                        }
                    }
                    ok &= (i - lst <= maxRange);

                    if (ok) maximize(dp[i], dp[j] + 1);
                }
            }
        }

        printf("%d", *max_element(dp + 1, dp + 1 + n));
    }
};

namespace subtask4 {
    bool check() {
        return (maxRange == 1);
    }

    int dp[MAX_N + 2];

    void solve() {
        int ans = 0;
        stack <int> st;
        dp[n] = 1;
        st.push(n);
        FORD(i, n - 1, 1) {
            while (!st.empty() && a[st.top()] <= a[i]) st.pop();
            dp[i] = 1 + (!st.empty() ? dp[st.top()] : 0);

            st.push(i);
        }

        printf("%d", *max_element(dp + 1, dp + 1 + n));
    }
};

namespace subtask5 {
    bool check() {
        return (maxRange == n);
    }

    void solve() {
        vector <int> v;
        FOR(i, 1, n) {
            if (v.empty()) v.push_back(a[i]);
            else {
                if (v.back() < a[i]) v.push_back(a[i]);
                else {
                    int id = lower_bound(ALL(v), a[i]) - v.begin();
                    v[id] = a[i];
                }
            }
        }

        cout << SZ(v);
    }
};

int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    cin >> n >> maxRange;
    vector <int> v;
    FOR(i, 1, n) cin >> a[i], v.push_back(a[i]);
    remove_dup(v);
    FOR(i, 1, n) a[i] = POS(a[i], v);

    if (subtask4 :: check()) return subtask4 :: solve(), 0;
    if (subtask5 :: check()) return subtask5 :: solve(), 0;
//    if (subtask1 :: check()) return subtask1 :: solve(), 0;
    subtask2 :: solve();

    return 0;
}

/* Discipline - Calm */
#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...