Submission #319005

# Submission time Handle Problem Language Result Execution time Memory
319005 2020-11-03T16:53:13 Z CodeTiger927 K blocks (IZhO14_blocks) C++14
0 / 100
3 ms 492 KB
using namespace std;

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

#define MAXN 10005
#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[105][MAXN],sum;

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[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][j] + arr[rightt[j]]);
			}else{
				update2(arr[j],dp[i - 1][j] + getMax(j + 1,MAXN));
			}
		}
		for(int j = 0;j < N;++j) {
			if(j < i) {
				dp[i][j] = 99999999999;
				continue;
			}
			dp[i][j] = dp[i - 1][i - 1] + getMax(i,j);
			if(leftt[i] != -1) {
				dp[i][j] = dp[i - 1][leftt[j]] + getMax(leftt[j] + 1,j);
				// if(i == 1 && j == 2) {
				// 	cout << getMax(2,2) << endl;
				// }
				if(leftt[leftt[i]] != -1) {
					dp[i][j] = min(dp[i][j],getMin(arr[leftt[leftt[j]]],MAXM));
				}
			}
		}
	}
	out << dp[K - 1][N - 1] << "\n";
	in.close();
	out.close();
	return 0;

}
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 492 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 492 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 492 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 492 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -