Submission #1105096

#TimeUsernameProblemLanguageResultExecution timeMemory
1105096manhlinh1501K blocks (IZhO14_blocks)C++17
100 / 100
366 ms50464 KiB
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 1e5 + 5;
const int MAXK = 105;
const int LOGN = 19;
const int oo32 = 1e9 + 5;
#define left ___left
int N, K;
int a[MAXN];
int RMQ[MAXN][LOGN];
int dp[MAXN][MAXK];
int left[MAXN];

int get_min(int l, int r) {
    int k = __lg(r - l + 1);
    return min(RMQ[l][k], RMQ[r - (1 << k) + 1][k]);
}

namespace solver {
    void solution() {
        dp[0][0] = 0;
        stack<int> S;
        int maxx = 0;
        for(int i = 1; i <= N; i++)
            dp[i][1] = (maxx = max(maxx, a[i]));

        for(int j = 2; j <= K; j++) {

            for(int i = 1; i <= N; i++)
                RMQ[i][0] = dp[i][j - 1];

            for(int j = 1; j < LOGN; j++) {
                for(int i = 1; i + (1 << j) - 1 <= N; i++)
                    RMQ[i][j] = min(RMQ[i][j - 1], RMQ[i + (1 << (j - 1))][j - 1]);
            }

            for(int i = 1; i <= N; i++) {
                if(left[i] < i)
                    dp[i][j] = min(dp[i][j], get_min(left[i], i - 1) + a[i]);

                dp[i][j] = min(dp[i][j], min(dp[left[i] - 1][j], dp[left[i] - 1][j - 1] + a[i]));
            }
        }

//        for(int i = 1; i <= N; i++) {
//            for(int j = 1; j <= K; j++)
//                cerr << i << " " << j << " " << dp[i][j] << "\n";
//        }
        cout << dp[N][K];
    }
}

signed main() {
#define task "code"
    if(fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> N >> K;
    for(int i = 1; i <= N; i++) cin >> a[i];
    for(int i = 1; i <= N; i++) {
        left[i] = i - 1;
        while(left[i] > 0 and a[left[i]] <= a[i])
            left[i] = left[left[i]];
    }
    for(int i = 1; i <= N; i++) left[i]++;
    memset(dp, 0x3f, sizeof dp);
    solver::solution();
    return (0 ^ 0);
}

Compilation message (stderr)

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