Submission #1287588

#TimeUsernameProblemLanguageResultExecution timeMemory
1287588datluong_04K blocks (IZhO14_blocks)C++20
100 / 100
146 ms80104 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define FOR(i , a , b) for(int i = a ; i <= b; i++)
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define maxn 100005
#define maxk 105

ll dp[maxk][maxn] , a[maxn];

int main(){
    if(fopen("blocks.in" , "r")){
        freopen("blocks.in" , "r" , stdin);
        freopen("blocks.out" , "w" , stdout);
    }
    
    FAST;
    int n , k;
    cin >> n >> k;
    FOR(i,  1 , n) cin >> a[i];
    FOR(i , 1 , k) FOR(j , 0 , n) dp[i][j] = 1e18;
    dp[1][0] = 0;
    FOR(i , 1 , n) dp[1][i] = max(dp[1][i - 1]  , a[i]);
    // dp(i , j) = min(dp(i - 1 , j') + max(a[j' + 1 , j])) j' < j

    FOR(i , 2 , k){
        stack<pair<ll,int>> st;
        FOR(j , i , n){
            ll minF = dp[i - 1][j - 1];
            while(!st.empty() && a[st.top().second] <= a[j]){
                minF = min(minF , st.top().first);
                st.pop();
            }
            dp[i][j] = min(dp[i][st.empty() ? 0 : st.top().second] , minF + a[j]);
            st.push({minF , j});
        }
    }

    cout << dp[k][n];   
}

Compilation message (stderr)

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