Submission #1217299

#TimeUsernameProblemLanguageResultExecution timeMemory
1217299lmaobruhK blocks (IZhO14_blocks)C++20
0 / 100
0 ms328 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define eb emplace_back
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define ve vector
#define vi ve<int>
#define vl ve<ll>
#define vpi ve<ii>
#define all(x) x.begin(), x.end()
#define fo(i,a,b) for (int i=(a),_b=(b); i<=_b; ++i)
#define fd(i,a,b) for (int i=(a),_b=(b); i>=_b; --i)
#define rep(i, n) for (int i = 1; i <= (n); ++i)
#define lc p+1
#define rc p+(mid-l+1)*2
#define maxi(a, b) a = max(a, b)
#define mini(a, b) a = min(a, b)
#define bit(s, i) (((s) >> (i)) & 1)

const int N = 1e5+5, NM = 1e6+5;
const int inf = 1e9;
const int mod = 1e9+7;
const int LG = 26, base=63;

int n, k, a[N];
vi f, g;

void go(int l, int r, int optl, int optr) {
    if (l > r) return;
    int mid = (l + r) >> 1, mx=a[mid], best=1e9, pos=l;
    fd(i,min(optr, mid),optl) {
        maxi(mx, a[i]);
        if (best>f[i-1]+mx) {
            best=f[i-1]+mx;
            pos=i;
        }
    }
    g[mid]=best;
    go(l, mid-1, optl, pos);
    go(mid+1, r, pos, optr);
}

void sol() {
    cin >> n >> k;
    f.resize(n+1,1e9), g.resize(n+1);
    int mx=0;
    fo(i,1,n) {
        cin >> a[i];
        maxi(mx,a[i]);
        f[i]=a[i];
    }
    fo(i,2,k) {
        go(1,n,1,n);
        swap(f, g);
        f[0]=1e9;
    }
    cout << f[n];
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    if (fopen("A.inp", "r")) {
        freopen("A.inp", "r", stdin);
        freopen("A.out", "w", stdout);
    }
    int tc = 1; // cin >> tc;
    fo(i,1,tc) sol();
}

Compilation message (stderr)

blocks.cpp: In function 'int main()':
blocks.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         freopen("A.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
blocks.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         freopen("A.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...