Submission #1039070

# Submission time Handle Problem Language Result Execution time Memory
1039070 2024-07-30T12:23:51 Z ttviet2805 K blocks (IZhO14_blocks) C++14
0 / 100
1 ms 604 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define II pair <int, int>
#define fi first
#define se second
#define all(S) S.begin(), S.end()

const int INF = 1e15;
const int N = 1e5 + 5;
int n, a[N], K;

void in() {
	cin >> n >> K;	
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector <vector <int>> f(K + 1, vector <int> (n + 5, INF));
	stack <II> st;
	f[0][0] = 0;
	a[0] = INF;
	for(int X = 1; X <= K; X++) {
		st.push({X - 1, f[X - 1][X - 1]});
		for(int i = X; i <= n; i++) {
			int curMin = INF;
			while(!st.empty() && a[i] >= a[st.top().fi]) {
				curMin = min(curMin, st.top().se);
				st.pop();
			}
			int res = min(curMin, f[X - 1][st.top().fi]);
			f[X][i] = f[X][st.top().fi];
			f[X][i] = min(f[X][i], res + a[i]);
			curMin = min(curMin, f[X - 1][i]);
			st.push({i, curMin});
		}
	}
	// for(int X = 1; X <= K; X++) {
	// 	for(int i = 1; i <= n; i++) {
	// 		cout << "#: " << X << ' ' << i << "\t" << f[X][i] << endl;
	// 	}
	// }
	cout << f[K][n] << '\n';
}

void process() {
	
}

int32_t main() {
	if(fopen("trash.inp", "r"))
		freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	int T = 1;
	while(T--) {
		in();
		process();
	}
}

Compilation message

blocks.cpp: In function 'int32_t main()':
blocks.cpp:52:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |   freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:52:44: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |   freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
      |                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Incorrect 0 ms 348 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 392 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Incorrect 0 ms 348 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Incorrect 0 ms 348 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Incorrect 0 ms 348 KB Output isn't correct
11 Halted 0 ms 0 KB -