답안 #1101485

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1101485 2024-10-16T09:00:38 Z LOLOLO K개의 묶음 (IZhO14_blocks) C++14
0 / 100
10 ms 86624 KB
#include <bits/stdc++.h>
#define int long long
#define f first
#define pii pair<int, int>
#define s second
 
const int N = 1e5 + 5;
const int block = 450;
const int oo = 99999989;
const int base = 31;
 
using namespace std;
 
int n, a[N], dp[N][105], rmq[N][20], k, L[N];
stack <int> st;

void build(int k){
	
	for(int i = 0;i <= n;i++) rmq[i][0] = dp[i][k]; 
	
	for(int j = 1;(1 << j) <= n;j++){
		for(int i = 0; i + (1 << j) - 1 <= n;i++){
			rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
		}
	}
	
}

int get(int l, int r){
	if(r == l) return rmq[l][0];
	int k = __lg(r - l + 1);
	return min(rmq[l][k], rmq[r - (1 << k) + 1][k]);
}
 
signed main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);cout.tie(0);
 
	
	cin >> n >> k;
	for(int i = 1;i <= n;i++) cin >> a[i];
	
	st.push(0); a[0] = 1e18;
	for(int i = 1;i <= n;i++){
			while(!st.empty() && a[st.top()] <= a[i]) st.pop();
			if(!st.empty()) L[i] = 1;
			else L[i] = st.top() + 1;
			st.push(i);
	}
	
	memset(dp, 0x4f, sizeof(dp));
	dp[0][0] = 0;
	
	for(int j = 1;j <= k;j++){
		build(j - 1);
		for(int i = j;i <= n;i++){
		  
			dp[i][j] = get(L[i] - 1, i - 1) + a[i];
			dp[i][j] = min(dp[i][j], dp[L[i] - 1][j]);
			
		}
		
	}
	
	cout << dp[n][k];
	return 0;
}
 
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 86608 KB Output is correct
2 Incorrect 10 ms 86552 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 86624 KB Output is correct
2 Incorrect 10 ms 86608 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 86608 KB Output is correct
2 Incorrect 10 ms 86552 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 86608 KB Output is correct
2 Incorrect 10 ms 86552 KB Output isn't correct
3 Halted 0 ms 0 KB -