Submission #1185846

#TimeUsernameProblemLanguageResultExecution timeMemory
1185846Celebi_276K blocks (IZhO14_blocks)C++20
100 / 100
129 ms2960 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1E5 + 15; long long dp[N][2]; int a[N], n, k; bool pre, cur; int32_t main () { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); dp[i][1] = max(dp[i - 1][1], 1LL * a[i]); } dp[0][1] = 1E18; pre = 1; cur = !pre; for (int j = 2; j <= k; j++) { for (int i = 0; i <= n; i++) dp[i][cur] = 0; stack <pair <int, long long>> T; for (int i = 0; i < j; i++) dp[i][cur] = 1e18; for (int i = j; i <= n; i++) { long long cur_mn = dp[i - 1][pre]; while (T.size() && a[T.top().first] <= a[i]) { cur_mn = min(cur_mn, T.top().second); T.pop(); } dp[i][cur] = cur_mn + a[i]; if (T.size()) dp[i][cur] = min(dp[i][cur], dp[T.top().first][cur]); T.emplace(i, cur_mn); } swap(cur, pre); } printf("%lld\n", dp[n][pre]); }

Compilation message (stderr)

blocks.cpp: In function 'int32_t main()':
blocks.cpp:8:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 |     scanf("%d%d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~
blocks.cpp:10:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |         scanf("%d", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...