Submission #932648

#TimeUsernameProblemLanguageResultExecution timeMemory
932648atomMoney (IZhO17_money)C++17
100 / 100
837 ms62136 KiB
#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;

#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif

signed main() {
    cin.tie(0) -> sync_with_stdio(0);

    int n;
    cin >> n;
    vector <int> a(n + 5);
    for (int i = 1; i <= n; ++i) cin >> a[i];

    vector <int> dp(n + 5, 1e9);
    set <int> S; // Maintaining outer-most segment
    S.insert(0); S.insert(1e9);
    dp[0] = 0;
    for (int i = 1; i <= n; ++i) {
        // Make a new segment. Any segment should be non-decreasing.
        dp[i] = min(dp[i], dp[i - 1] + 1);
        // Continue current segment
        // The outer-most segment is [l, r], any segment (i..j) is valid if l <= a(i) <= r.
        int l = *(--S.upper_bound(a[i]));
        int r = *(S.upper_bound(a[i]));
        int j = i + 1;
        while (j <= n && l <= a[j] && a[j] <= r && a[j - 1] <= a[j]) ++j;

        debug(j, dp[i], l, r);
        for (int k = i; k < j; ++k) dp[k] = min(dp[k], dp[i - 1] + 1);
        S.insert(a[i]);
    }
    cout << dp[n] << "\n";
}


Compilation message (stderr)

money.cpp: In function 'int main()':
money.cpp:9:20: warning: statement has no effect [-Wunused-value]
    9 | #define debug(...) 166
      |                    ^~~
money.cpp:34:9: note: in expansion of macro 'debug'
   34 |         debug(j, dp[i], l, r);
      |         ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...