Submission #91380

#TimeUsernameProblemLanguageResultExecution timeMemory
91380Just_Solve_The_ProblemK blocks (IZhO14_blocks)C++11
100 / 100
168 ms13668 KiB
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 7; const int inf = (int)1e9 + 7; int n, k; int dp[2][N]; int a[N]; main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } stack < pair < int, int > > st; for (int i = 1; i <= n; i++) { dp[1][i] = max(dp[1][i - 1], a[i]); } for (int i = 2; i <= k; i++) { while (!st.empty()) st.pop(); st.push({dp[i & 1 ^ 1][i - 1], a[i]}); dp[i & 1][i] = dp[i & 1 ^ 1][i - 1] + a[i]; for (int j = i + 1; j <= n; j++) { int c = dp[i & 1 ^ 1][j - 1]; while (!st.empty() && st.top().second <= a[j]) { c = min(c, st.top().first); st.pop(); } if (st.empty() || st.top().first + st.top().second > c + a[j]) { st.push({c, a[j]}); } dp[i & 1][j] = st.top().first + st.top().second; } } cout << dp[k & 1][n]; }

Compilation message (stderr)

blocks.cpp:11:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
blocks.cpp: In function 'int main()':
blocks.cpp:22:17: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   st.push({dp[i & 1 ^ 1][i - 1], a[i]});
               ~~^~~
blocks.cpp:23:23: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   dp[i & 1][i] = dp[i & 1 ^ 1][i - 1] + a[i];
                     ~~^~~
blocks.cpp:25:17: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
    int c = dp[i & 1 ^ 1][j - 1];
               ~~^~~
blocks.cpp:12:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~~
blocks.cpp:14:8: 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...