Submission #1105089

# Submission time Handle Problem Language Result Execution time Memory
1105089 2024-10-25T10:18:28 Z manhlinh1501 K blocks (IZhO14_blocks) C++17
0 / 100
6 ms 41720 KB
#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;

int N, K;
int a[MAXN];
int RMQ[MAXN][LOGN];
int dp[MAXN][MAXK];

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

namespace subtask {
    void solution() {
        dp[0][0] = 0;
        for(int i = 1; i <= N; i++) {
            for(int j = 1; j <= i; j++) {
                for(int z = 1; z <= K; z++) {
                    dp[i][z] = min(dp[i][z], dp[j - 1][z - 1] + get_max(j, i));
                }
            }
        }
        cout << dp[N][K] << "\n";
    }
}

namespace solver {
    void solution() {
        dp[0][0] = 0;
        stack<int> S;
        a[0] = oo32;
        S.emplace(0);
        for(int i = 1; i <= N; i++) {
            while(S.size() > K and a[S.top()] <= a[i]) {
                int p = S.top();
                S.pop();
                for(int j = 1; j <= K; j++)
                    dp[i][j] = min(dp[i][j], dp[p - 1][j - 1] + a[i]);
            }
            int p = (S.empty() ? 0 : S.top());
            for(int j = 1; j <= K; j++)
                dp[i][j] = min(dp[i][j], dp[p][j - 1] + a[i]);
            S.emplace(i);
        }
        cout << dp[N][K];
    }
}

signed main() {
#define task ""
    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++) RMQ[i][0] = a[i];
    for(int j = 1; j < LOGN; j++) {
        for(int i = 1; i + (1 << j) - 1 <= N; i++)
            RMQ[i][j] = max(RMQ[i][j - 1], RMQ[i + (1 << (j - 1))][j - 1]);
    }
    memset(dp, 0x3f, sizeof dp);
    solver::solution();
    return (0 ^ 0);
}

Compilation message

blocks.cpp: In function 'void solver::solution()':
blocks.cpp:40:28: warning: comparison of integer expressions of different signedness: 'std::stack<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   40 |             while(S.size() > K and a[S.top()] <= a[i]) {
      |                   ~~~~~~~~~^~~
blocks.cpp: In function 'int main()':
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".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 41552 KB Output is correct
2 Incorrect 5 ms 41552 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 41720 KB Output is correct
2 Incorrect 5 ms 41552 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 41552 KB Output is correct
2 Incorrect 5 ms 41552 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 41552 KB Output is correct
2 Incorrect 5 ms 41552 KB Output isn't correct
3 Halted 0 ms 0 KB -