Submission #171745

# Submission time Handle Problem Language Result Execution time Memory
171745 2019-12-30T09:04:21 Z VEGAnn K blocks (IZhO14_blocks) C++14
0 / 100
41 ms 5496 KB
#include <bits/stdc++.h>
using namespace std;
const int oo = 2e9;
const int N = 100100;
const int K = 110;
const int PW = 20;
int f[N][K], a[N], n, k, sp[N][PW], prec[N];
bool bad = 0;

int get_max(int l, int r){
    if (l > r) swap(l, r);
    int len = r - l + 1;
    return max(sp[l][prec[len]], sp[r - (1 << prec[len]) + 1][prec[len]]);
}

void calc(int j, int l, int r, int optl, int optr){
    if (l > r) return;
    int md = (l + r) >> 1;
    int mn = oo, ps = -1;
    for (int i = optl; i <= min(md, optr); i++){
        int mx = get_max(i, md);
        if (f[i - 1][j - 1] < oo &&
            mn >= f[i - 1][j - 1] + mx){
              mn = f[i - 1][j - 1] + mx;
              ps = i;
        }
    }
    if (mn == oo) bad = 1;
    f[md][j] = mn;
    calc(j, l, md - 1, optl, ps);
    calc(j, md + 1, r, ps, optr);
}

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

    prec[0] = -1;
    for (int i = 1; i <= n; i++)
        prec[i] = prec[i >> 1] + 1;

    for (int i = 1; i <= n; i++)
        sp[i][0] = a[i];

    for (int po = 1; po < PW; po++)
    for (int i = 1; i <= n; i++){
        if (i + (1 << po) - 1 > n) continue;
        sp[i][po] = max(sp[i][po - 1], sp[i + (1 << (po - 1))][po - 1]);
    }

    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= k; j++)
            f[i][j] = oo;

    f[0][0] = 0;

    for (int j = 1; j <= k; j++)
        calc(j, 1, n, 1, n);

    if (bad) return -1;

    cout << f[n][k];
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Runtime error 2 ms 376 KB Execution failed because the return code was nonzero
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Runtime error 2 ms 376 KB Execution failed because the return code was nonzero
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 380 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 41 ms 5496 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -