Submission #1198756

#TimeUsernameProblemLanguageResultExecution timeMemory
11987564o2aK blocks (IZhO14_blocks)C++17
53 / 100
1099 ms82488 KiB
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define _F "test"
using namespace std;
typedef long long ll;

constexpr ll INF = 1e18;
ll st[400001];

void upd(int v, int tl, int tr, int pos, ll val)
{
    if (tl == tr)
        st[v] = val;
    else
    {
        int tm = (tl+tr)/2;
        if (pos <= tm)
            upd(2*v, tl, tm, pos, val);
        else
            upd(2*v+1, tm+1, tr, pos, val);
        st[v] = min(st[2*v], st[2*v+1]);
    }
}

ll getmin(int v, int tl, int tr, int l, int r)
{
    if (l > r)
        return INF;
    if (l <= tl && tr <= r)
        return st[v];
    int tm = (tl+tr)/2;
    return min(getmin(2*v, tl, tm, l, min(r, tm)),
               getmin(2*v+1, tm+1, tr, max(l, tm+1), r));
}

void solve()
{
    int n, k, cur = 0, app = 1; cin>>n>>k;
    vector<int> val(n+1), pre(n+1); val[0] = 10000000;
    stack<int> temp; temp.push(0);
    vector<vector<ll>> dp(k+1, vector<ll>(n+1, INF)); dp[0][0] = 0;
    for (int i = 1; i <= n; ++i)
    {
        cin>>val[i];
        while (val[i] >= val[temp.top()])
            temp.pop();
        pre[i] = temp.top();
        temp.push(i);
    }
    dp[1][1] = val[1];
    for (int i = 2; i <= n; ++i)
        dp[1][i] = max(dp[1][i-1], (ll)val[i]);
    for (int i = 2; i <= k; ++i)
    {
        for (int j = 1; j <= n; ++j)
            upd(1, 1, n, j, val[j] + dp[i-2][j-1]);
        for (int j = i; j <= n; ++j)
        {
            dp[i][j] = min({dp[i][pre[j]],
                            dp[i-1][j-1] + val[j],
                            val[j] + getmin(1, 1, n, pre[j]+1, j-1)});
        }
    }
    cout<<dp[k][n];
}

int main()
{
    if (fopen(_F".INP", "r"))
    {
        freopen(_F".INP", "r", stdin);
        //freopen(_F".OUT", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int Test = 1; //cin>>Test;
    while (Test--) solve();
}

Compilation message (stderr)

blocks.cpp: In function 'int main()':
blocks.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen(_F".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...