Submission #815149

#TimeUsernameProblemLanguageResultExecution timeMemory
815149RecursiveCoFinancial Report (JOI21_financial)C++14
48 / 100
4046 ms82444 KiB
// CF template, version 3.0

#include <bits/stdc++.h>

using namespace std;

#define improvePerformance ios_base::sync_with_stdio(false); cin.tie(0)
#define getTest int t; cin >> t
#define eachTest for (int _var=0;_var<t;_var++)
#define get(name) int (name); cin >> (name)
#define out(o) cout << (o)
#define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
#define sortl(name) sort((name).begin(), (name).end())
#define rev(name) reverse((name).begin(), (name).end())
#define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
#define decision(b) if (b){out("YES");}else{out("NO");}

#define int long long int

int query(vector<vector<int>>& sparse, int l, int r) {
    int sz = r - l + 1;
    int lg = 0;
    int p = 1;
    while (p <= sz) {
        p *= 2;
        lg++;
    }
    p /= 2;
    lg--;
    int left = sparse[l][lg];
    int right = sparse[r - p + 1][lg];
    return max(left, right);
}

signed main() {
    improvePerformance;

    get(n);
    get(d);
    getList(n, nums);
    vector<int> minimums(n, 1e18);
    multiset<int> window;
    forto(d, i) window.insert(nums[n - i - 1]);
    for (int i = n - 1; i >= d - 1; i--) {
        minimums[i] = *window.begin();
        window.erase(window.find(nums[i]));
        if (i != d - 1) window.insert(nums[i - d]);
    }
    vector<vector<int>> sparse(n, vector<int>(19, 0));
    for (int i = n - 1; i >= 0; i--) {
        sparse[i][0] = minimums[i];
        int p = 2;
        forto(19, j) {
            if (j == 0) continue;
            if (i + p > n) break;
            sparse[i][j] = max(sparse[i][j - 1], sparse[i + p / 2][j - 1]);
            p *= 2;
        }
    }
    vector<int> dp;
    forto(n, i) dp.push_back(0);
    int prefmax = 0;
    for (int i = n - 1; i >= 0; i--) {
        if (prefmax < nums[i]) {
            dp[i] = 1;
        } else {
            // what's the smallest index j such that j >= i+d and minimums[j] > a[i]?
            // then we can take any element in between.
            // (sparse table for the first part)
            // what about the second part, "taking any element in between"?
            int l = min(i + d, n);
            int r = n;
            while (r - l >= 1) {
                int middle = (l + r) / 2;
                if (query(sparse, i + d, middle) <= nums[i]) l = middle + 1;
                else r = middle;
            }
            // inefficient for now
            int maximum = 0;
            for (int j = i + 1; j <= l; j++) {
                if (nums[j] > nums[i]) maximum = max(maximum, dp[j]);
            }
            dp[i] = maximum + 1;
        }
        prefmax = max(prefmax, nums[i]);
    }
    out(*max_element(dp.begin(), dp.end()));
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:10:23: warning: unnecessary parentheses in declaration of 'n' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
Main.cpp:38:5: note: in expansion of macro 'get'
   38 |     get(n);
      |     ^~~
Main.cpp:10:23: warning: unnecessary parentheses in declaration of 'd' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
Main.cpp:39:5: note: in expansion of macro 'get'
   39 |     get(d);
      |     ^~~
Main.cpp:12:40: warning: unnecessary parentheses in declaration of 'nums' [-Wparentheses]
   12 | #define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
      |                                        ^
Main.cpp:40:5: note: in expansion of macro 'getList'
   40 |     getList(n, nums);
      |     ^~~~~~~
Main.cpp:10:23: warning: unnecessary parentheses in declaration of 'a' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
Main.cpp:12:76: note: in expansion of macro 'get'
   12 | #define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
      |                                                                            ^~~
Main.cpp:40:5: note: in expansion of macro 'getList'
   40 |     getList(n, nums);
      |     ^~~~~~~
Main.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
Main.cpp:43:5: note: in expansion of macro 'forto'
   43 |     forto(d, i) window.insert(nums[n - i - 1]);
      |     ^~~~~
Main.cpp:15:35: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
Main.cpp:53:9: note: in expansion of macro 'forto'
   53 |         forto(19, j) {
      |         ^~~~~
Main.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
Main.cpp:61:5: note: in expansion of macro 'forto'
   61 |     forto(n, i) dp.push_back(0);
      |     ^~~~~
#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...