Submission #737686

#TimeUsernameProblemLanguageResultExecution timeMemory
737686pubin06K blocks (IZhO14_blocks)C++14
0 / 100
1 ms340 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define sz(v) (int)(v).size()
using namespace std;

const int MAXn = 100005;
const int oo = 1e9;

int N, K, a[MAXn], Lf[MAXn], dp[MAXn][102];
int tmp[MAXn];

signed main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    
    #define TASK "BLOCKS"
    if (ifstream(TASK".INP")) {
    	freopen(TASK".INP", "r", stdin);
    	freopen(TASK".OUT", "w", stdout);
    }
    
    cin >> N >> K;
    for (int i = 1; i <= N; i++) cin >> a[i];
    stack<int> st;
    a[0] = 1000001;
    st.push(0);
    for (int i = 1; i <= N; i++) {
    	while (a[st.top()] <= a[i]) st.pop();
    	Lf[i] = st.top();
    	st.push(i);
    	// cerr << Lf[i] << ' ';
    }
    // cerr << "\n\n";
    tmp[0] = oo;
    for (int i = 1; i <= N - K + 1; i++) dp[i][1] = max(dp[i - 1][1], a[i]);
    tmp[1] = oo;
    for (int i = 2; i <= N - K + 2; i++) {
    	if (Lf[i]) {
    		tmp[i] = min(tmp[Lf[i]], dp[Lf[i]][1] + a[i]);
    	} else {
    		tmp[i] = dp[1][1] + a[i];
    	}
    }
    for (int j = 2; j <= K; j++) {
    	for (int i = j; i <= N - K + j; i++) {
    		dp[i][j] = tmp[i];
    	}
    	tmp[j] = oo;
    	for (int i = j + 1; i <= N - K + j + 1; i++) {
    		if (Lf[i] >= j) {
	    		tmp[i] = min(tmp[Lf[i]], dp[Lf[i]][j] + a[i]);
	    	} else {
	    		tmp[i] = dp[j][j] + a[i];
	    	}
    	}
    }
    // for (int j = 1; j <= K; j++) {
    	// for (int i = 1; i <= N; i++) cerr << dp[i][j] << ' ';
    	// cerr << '\n';
    // }
    cout << dp[N][K];
    
    return 0;
}
// Revive! >:)
// IZhO 2014 - Day 2 - Problem E

Compilation message (stderr)

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