Submission #1016037

#TimeUsernameProblemLanguageResultExecution timeMemory
1016037ngothecuong07K blocks (IZhO14_blocks)C++14
100 / 100
167 ms84680 KiB
#include<bits/stdc++.h> #define int long long #define pi pair<int, int> using namespace std; const int N = 1e5 + 5, K = 1e2 + 5, INF = 1e17; int n, k; int a[N]; int f[N][K]; signed main() { cin.tie(0)->sync_with_stdio(0); if(fopen("task.inp", "r")) { freopen("task.inp", "r", stdin); freopen("task.out", "w", stdout); } if(fopen("KBLOCK.inp", "r")) { freopen("KBLOCK.inp", "r", stdin); freopen("KBLOCK.out", "w", stdout); } cin >> n >> k; for(int i = 1; i <= n; ++i) cin >> a[i]; for(int i = 0; i <= n; ++i) for(int j = 0; j <= k; ++j) f[i][j] = INF; f[1][1] = a[1]; for(int i = 2; i <= n; ++i) f[i][1] = max(f[i - 1][1], a[i]); for(int j = 2; j <= k; ++j) { stack<pi> st; for(int i = j; i <= n; ++i) { int mi = f[i - 1][j - 1]; while(st.size() && a[st.top().first] <= a[i]) { mi = min(mi, st.top().second); st.pop(); } f[i][j] = min(f[st.size() ? st.top().first : 0][j], mi + a[i]); st.push({i, mi}); } } cout << f[n][k]; }

Compilation message (stderr)

blocks.cpp: In function 'int main()':
blocks.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |     freopen("task.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |     freopen("task.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:23:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |     freopen("KBLOCK.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:24:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |     freopen("KBLOCK.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...