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() {
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) & 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));
}
}
}
}
out << dp[(K - 1) & 1][N - 1] << "\n";
in.close();
out.close();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 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 |
364 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 |
- |