Submission #1040599

#TimeUsernameProblemLanguageResultExecution timeMemory
1040599Yang8onK blocks (IZhO14_blocks)C++14
100 / 100
113 ms85336 KiB
#include <bits/stdc++.h>
#define Y8o "K blocks"
#define maxn (int) 1e5 + 5
#define ll long long
#define pii pair<int, ll>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r

#define f first
#define s second

using namespace std;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
    return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
    if(fopen(Y8o".in", "r"))
    {
        freopen(Y8o".in", "r", stdin);
        freopen(Y8o".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(NULL), cout.tie(NULL);
}
void ctime() {
    cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}


int n, k;
int a[maxn];
ll dp[105][maxn];

pii q[maxn]; /// f/s : vtri/gia tri dp[gr - 1] min tu q[c - 1] den q[c] - 1
void calc(int gr) { /// update for dp[gr][i]
    int d = 1, c = 0;
    for(int i = 1; i <= n; i ++) {
        ll mi = dp[gr - 1][i - 1];
        while(d <= c && a[i] >= a[q[c].f]) {
            mi = min(mi, q[c].s);
            c --;
        }
        dp[gr][i] = mi + a[i];
        if(c) dp[gr][i] = min(dp[gr][i], dp[gr][q[c].f]);
        q[++ c] = {i, mi};
    }
}

void solve()
{
    cin >> n >> k;
    for(int i = 1; i <= n; i ++) cin >> a[i];

    memset(dp, 0x3f, sizeof dp);
    dp[0][0] = 0;

    for(int i = 1; i <= k; i ++) calc(i);
    cout << dp[k][n];
}


int main()
{
    iof();

    int nTest = 1;
//    cin >> nTest;

    while(nTest --) {
        solve();
    }

    ctime();
    return 0;
}

Compilation message (stderr)

blocks.cpp: In function 'void iof()':
blocks.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         freopen(Y8o".in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:24:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         freopen(Y8o".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...