답안 #284347

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
284347 2020-08-27T09:05:42 Z shivensinha4 K개의 묶음 (IZhO14_blocks) C++17
0 / 100
722 ms 5496 KB
#include <bits/stdc++.h> 
using namespace std; 
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
#define endl '\n'


int main() {
	#ifndef ONLINE_JUDGE
	//freopen("test.in", "r", stdin);
	#endif
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int n, k; cin >> n >> k;
	vector<ll> nums(n);
	for_(i, 0, n) cin >> nums[i];
	vector<ll> prev(n), curr(n, 1e15);
	prev[n-1] = nums[n-1];
	for (int i = n-2; i >= 0; i--) prev[i] = max(nums[i], prev[i+1]);
	for_(l, 1, k) {
		stack<vector<ll>> s; // {max val index, min prev val till next max}
		s.push({n-l-1, prev[n-l]});
		for (int i = n-l-2; i >= 0; i--) {
			ll mn = prev[i+1], ans = prev[i+1]+nums[i];
			while (s.size()) {
				if (nums[s.top()[0]] <= nums[i]) {
					mn = min(mn, s.top()[1]);
					s.pop();
				} else {
					ans = min(ans, curr[s.top()[0]]);
					break;
				}
			}
			
			s.push({i, mn});
			curr[i] = min(ans, nums[i] + mn);
		}
		swap(prev, curr);
		curr.assign(n, 1e15);
	}
	
	ll ans = 1e15;
	for_(i, 0, n-k+1) ans = min(ans, prev[i]);
	cout << ans << endl;
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 1 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 0 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 896 KB Output is correct
2 Correct 6 ms 1664 KB Output is correct
3 Correct 24 ms 2560 KB Output is correct
4 Correct 227 ms 2560 KB Output is correct
5 Correct 722 ms 5496 KB Output is correct
6 Incorrect 25 ms 5376 KB Output isn't correct
7 Halted 0 ms 0 KB -