Submission #638342

#TimeUsernameProblemLanguageResultExecution timeMemory
638342nguyentunglamK blocks (IZhO14_blocks)C++14
100 / 100
196 ms44832 KiB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define forin(i, a, b) for(int i = a; i <= b; i++)
#define forde(i, a, b) for(int i = a; i >= b; i--)
#define forv(a, b) for(auto & a : b)
#define fi first
#define se second
#define ii pair<int, int>
#define endl "\n"
using namespace std;
const int N = 1e5 + 10;
int a[N], f[N][110];
int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int n, k; cin >> n >> k;
    for(int i = 1; i <= n; i++) cin >> a[i];
    memset(f, 127, sizeof(f));
    f[0][0] = 0;
    for(int j = 1; j <= k; j++) {
        stack<pair<int, int>> s;
        for(int i = 1; i <= n; i++) {
            int g = f[i - 1][j - 1];
            while (!s.empty() && a[s.top().fi] <= a[i]) {
                g = min(g, s.top().se);
                s.pop();
            }
            f[i][j] = g + a[i];
            if (!s.empty()) f[i][j] = min(f[i][j], f[s.top().fi][j]);
            s.push({i, g});
        }
    }
    cout << f[n][k];
}

Compilation message (stderr)

blocks.cpp: In function 'int main()':
blocks.cpp:17:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:18:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:21:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:22:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         freopen (task".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...