Submission #95437

#TimeUsernameProblemLanguageResultExecution timeMemory
95437choikiwonK blocks (IZhO14_blocks)C++17
100 / 100
183 ms2300 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int MN = 100010; int N, K; int A[MN], dp[2][MN]; struct Info { int h, mn, id; }; int main() { scanf("%d %d", &N, &K); for(int i = 0; i < N; i++) { scanf("%d", &A[i]); } for(int i = N - 1; i >= 0; i--) { dp[1][i] = A[i]; dp[1][i] = max(dp[1][i], dp[1][i + 1]); } for(int i = 2; i <= K; i++) { int p = (i & 1) ^ 1; int c = (i & 1); stack<Info> stk1; stack<pii> stk2; for(int j = N - i; j >= 0; j--) { int mn = dp[p][j + 1]; while(!stk1.empty() && stk1.top().h <= A[j]) { mn = min(mn, stk1.top().mn); stk1.pop(); } int t = stk1.empty()? N - i + 1 : stk1.top().id; while(!stk2.empty() && stk2.top().second < t) { stk2.pop(); } stk1.push({A[j], mn, j}); if(stk2.empty() || stk2.top().first > mn + A[j]) { stk2.push({mn + A[j], j}); } dp[c][j] = stk2.top().first; //cout << i << ' ' << j << ' ' << dp[c][j] << endl; } } printf("%d", dp[K & 1][0]); }

Compilation message (stderr)

blocks.cpp: In function 'int main()':
blocks.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &N, &K);
     ~~~~~^~~~~~~~~~~~~~~~~
blocks.cpp:20:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         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...