답안 #319016

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
319016 2020-11-03T17:06:32 Z CodeTiger927 K개의 묶음 (IZhO14_blocks) C++14
0 / 100
12 ms 16888 KB
using namespace std;

#include <iostream>
#include <fstream>
#include <stack>
#include <cstring>

#define MAXN 100005
#define MAXM 1000005

long long st[262144];
long long st2[2097152];

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);
}

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

void update2(int x,long long v) {
	update2(x,v,0,MAXM,0);
}

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

long long getMin(int a,int b) {
	return getMin(a,b,0,MAXM,0);
}

int N,K,leftt[MAXN],rightt[MAXN];
long long arr[MAXN],dp[2][MAXN],sum;

int main() {
	cin >> N >> K;
	for(int i = 0;i < N;++i) {
		cin >> arr[i];
		update(i,arr[i]);
		sum += arr[i];
		dp[0][i] = arr[i];
		if(i != 0) dp[0][i] = max(dp[0][i],dp[0][i - 1]);
	}

	stack<int> s;
	s.push(0);
	for(int i = 1;i < N;++i) {
		while(!s.empty() && arr[s.top()] > arr[i]) {
			rightt[s.top()] = i;
			s.pop();
		}
		s.push(i);
	}
	while(!s.empty()) {
		rightt[s.top()] = -1;
		s.pop();
	}

	s.push(N - 1);
	for(int i = N - 2;i >= 0;--i) {
		while(!s.empty() && arr[s.top()] < arr[i]) {
			leftt[s.top()] = i;
			s.pop();
		}
		s.push(i);
	}
	while(!s.empty()) {
		leftt[s.top()] = -1;
		s.pop();
	}

	for(int i = 1;i < K;++i) {
		memset(st2,63,sizeof(st2));
		for(int j = 0;j < N;++j) {
			if(rightt[j] != -1) {
				update2(arr[j],dp[(i - 1) & 1][j] + arr[rightt[j]]);
			}else{
				update2(arr[j],dp[(i - 1) & 1][j] + getMax(j + 1,MAXN));
			}
		}
		for(int j = 0;j < N;++j) {
			if(j < i) {
				dp[i & 1][j] = 99999999999;
				continue;
			}
			dp[i & 1][j] = dp[(i - 1) & 1][i - 1] + getMax(i,j);
			if(leftt[j] != -1) {
				dp[i & 1][j] = min(dp[i & 1][j],dp[(i - 1) & 1][leftt[j]] + getMax(leftt[j] + 1,j));
				if(leftt[leftt[j]] != -1) {
					dp[i & 1][j] = min(dp[i & 1][j],getMin(arr[leftt[leftt[j]]],MAXM));
				}
			}
		}
	}
	cout << dp[(K - 1) & 1][N - 1] << endl;

	return 0;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 9 ms 16768 KB Output is correct
5 Correct 9 ms 16760 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 9 ms 16888 KB Output is correct
8 Correct 10 ms 16748 KB Output is correct
9 Incorrect 12 ms 16876 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 10 ms 16748 KB Output is correct
5 Correct 9 ms 16748 KB Output is correct
6 Correct 1 ms 396 KB Output is correct
7 Correct 9 ms 16748 KB Output is correct
8 Correct 10 ms 16748 KB Output is correct
9 Incorrect 11 ms 16748 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 9 ms 16768 KB Output is correct
5 Correct 9 ms 16760 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 9 ms 16888 KB Output is correct
8 Correct 10 ms 16748 KB Output is correct
9 Incorrect 12 ms 16876 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 9 ms 16768 KB Output is correct
5 Correct 9 ms 16760 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 9 ms 16888 KB Output is correct
8 Correct 10 ms 16748 KB Output is correct
9 Incorrect 12 ms 16876 KB Output isn't correct
10 Halted 0 ms 0 KB -