Submission #363312

#TimeUsernameProblemLanguageResultExecution timeMemory
363312nicolaalexandraK blocks (IZhO14_blocks)C++14
53 / 100
1091 ms82156 KiB
#include <bits/stdc++.h>
#define DIM 100010
#define DIMK 110
#define INF 2000000000000000000LL
using namespace std;

long long dp[DIMK][DIM],aint[DIM*4],v[DIM];
deque <int> d;
int n,k,i,j,t;


void build (int nod, int st, int dr){
    if (st == dr){
        aint[nod] = dp[j-1][st];
        return;
    }
    int mid = (st+dr)>>1;
    build (nod<<1,st,mid);
    build ((nod<<1)|1,mid+1,dr);
    aint[nod] = min (aint[nod<<1],aint[(nod<<1)|1]);
}

int query (int nod, int st, int dr, int x, int y){
    if (x <= st && dr <= y)
        return aint[nod];

    int mid = (st+dr)>>1;
    long long sol_st = INF, sol_dr = INF;
    if (x <= mid)
        sol_st = query (nod<<1,st,mid,x,y);
    if (y > mid)
        sol_dr = query ((nod<<1)|1,mid+1,dr,x,y);
    return min (sol_st,sol_dr);
}

int main (){

    //ifstream cin ("date.in");
    //ofstream cout ("date.out");

    cin>>n>>k;

    for (i=1;i<=k;i++)
        for (j=1;j<=n;j++)
            dp[i][j] = INF;

    for (i=1;i<=n;i++){
        cin>>v[i];
        dp[1][i] = max (dp[1][i-1],v[i]);
    }

    for (j=2;j<=k;j++){

        build (1,1,n);

        d.clear();
        for (i=j;i<=n;i++){

            while (!d.empty() && v[i] >= v[d.back()])
                d.pop_back();

            if (d.empty())
                dp[j][i] = v[i] + query (1,1,n,1,i-1);
            else dp[j][i] = min (dp[j][d.back()],v[i] + query (1,1,n,d.back(),i-1));

            d.push_back(i);
        }
    }

    cout<<dp[k][n];

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...