Submission #882260

# Submission time Handle Problem Language Result Execution time Memory
882260 2023-12-02T21:05:35 Z OAleksa K blocks (IZhO14_blocks) C++14
0 / 100
1 ms 4444 KB
#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
const int maxn = 1e5 + 69;
const int inf = 1e9;
int st[4 * maxn], n, k, a[maxn], dp[maxn], d[maxn];
void upd(int v, int tl, int tr, int p, int x) {
	if (tl == tr)
		st[v] = x;
	else {
		int mid = (tl + tr) / 2;
		if (p <= mid)
			upd(v * 2, tl, mid, p, x);
		else
			upd(v * 2 + 1, mid + 1, tr, p, x);
		st[v] = min(st[v * 2], st[v * 2 + 1]);
	}
}
int qry(int v, int tl, int tr, int l, int r) {
	if (tl > r || tr < l)
		return 1e9;
	else if (tl >= l && tr <= r)
		return st[v];
	else {
		int mid = (tl + tr) / 2;
		return min(qry(v * 2, tl, mid, l, r), qry(v * 2 + 1, mid + 1, tr, l, r));
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
  int tt = 1;
  //cin >> tt;
  while (tt--) {
   	cin >> n >> k;
   	for (int i = 1;i <= n;i++)
   		cin >> a[i];
   	
   	vector<int> st;
   	for (int i = 1;i <= n;i++) {
			while (!st.empty() && a[st.back()] < a[i]) 
				st.pop_back();
			if (!st.empty())
				d[i] = st.back();
			st.push_back(i);
   	}
   	dp[0] = inf;
   	for (int j = 1;j <= k;j++) {
   		for (int i = 1;i <= n;i++)
   			upd(1, 1, n, i, dp[i]);
   		for (int i = 1;i <= n;i++) {
   			if (j > i)
   				dp[i] = inf;
   			dp[i] = min(dp[d[i]], qry(1, 1, n, d[i] + 1, i) + a[i]);
   		}
   	}
   	cout << dp[n];
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Incorrect 1 ms 4440 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Incorrect 1 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Incorrect 1 ms 4440 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Incorrect 1 ms 4440 KB Output isn't correct
3 Halted 0 ms 0 KB -