답안 #318739

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
318739 2020-11-03T04:59:16 Z CodeTiger927 K개의 묶음 (IZhO14_blocks) C++14
0 / 100
1 ms 384 KB
using namespace std;

#include <iostream>
#include <fstream>

#define MAXN 100005

long long st[262144];

void update(int x,long long v,int l,int r,int p) {
	if(l > x || r < x) return;
	if(l == r) {
		st[p] = v;
		return;
	}
	int m = (l + r) / 2;
	update(x,v,l,m,2 * p + 1);
	update(x,v,m + 1,r,2 * p + 2);
	st[p] = max(st[2 * p + 1],st[2 * p + 2]);
	return;
}

void update(int x,long long v) {
	update(x,v,0,MAXN,0);
}

long long getMax(int a,int b,int l,int r,int p) {
	if(l > b || r < a) return -99999999999;
	if(l >= a && r <= b) return st[p];
	int m = (l + r) / 2;
	return max(getMax(a,b,l,m,2 * p + 1),getMax(a,b,m + 1,r,2 * p + 2));
}

long long getMax(int a,int b) {
	return getMax(a,b,0,MAXN,0);
}

int N,K;
long long arr[MAXN],dp[MAXN][105],sum;

void solve(int lvl,int l,int r,int x,int y) {
	// cout << l << " " << r << endl;
	if(l < 0 || r >= N || x < 0 || y >= N || l > r || x > y) return;

	int m = (l + r) / 2;
	// cout << l << " " << r << " " << m << " " << lvl << endl;
	
	long long best = sum;
	int re = -1;
	for(int i = x;i <= y;++i) {
		if(i >= m) continue;
		long long cur = getMax(i + 1,m) + dp[i][lvl - 1];
		if(cur < best) {
			best = cur;
			re = i;
		}
	}
	dp[m][lvl] = best;
	// cout << m << " " << lvl << " " << re << " " << best << " " << endl;
	if(re == -1) {
		re = x;
	}
	if(l == r) return;
	solve(lvl,l,m - 1,x,re);
	solve(lvl,m + 1,r,re,y);
}

int main() {
	ifstream in("blocks.in");
	ofstream out("blocks.out");

	in >> N >> K;
	for(int i = 0;i < N;++i) {
		in >> arr[i];
		update(i,arr[i]);
		sum += arr[i];
		dp[i][0] = arr[i];
		if(i != 0) dp[i][0] = max(dp[i][0],dp[i - 1][0]);
	}
	for(int i = 1;i < K;++i) {
		solve(i,0,N - 1,0,N - 1);
	}
	out << dp[N - 1][K - 1] << endl;
	// cout << dp[1][1] << endl;
	in.close();
	out.close();
	return 0;

}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 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 Incorrect 1 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -