#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int INF = 1e9;
const int MAXN = 1e5 + 10;
const int MAXK = 101;
int vetor[MAXN],dp[MAXN],esq[MAXN],dir[MAXN],N,K;
ii seg[4*MAXN];
void build(int pos,int left,int right){
//printf("Build1 %d %d %d\n",pos,left,right);
if(left == right){
seg[pos] = ii(dp[left],left);
return;
}
int mid = (left+right)/2;
build(2*pos,left,mid);
build(2*pos+1,mid+1,right);
seg[pos] = max(seg[2*pos],seg[2*pos+1]);
}
ii query(int pos,int left,int right,int i,int j){
//printf("Query1 %d %d %d %d\n",pos,left,right,i,j);
if(left >= i && right <= j) return seg[pos];
int mid = (left+right)/2;
if(j <= mid) return query(2*pos,left,mid,i,j);
else if(i >= mid + 1) return query(2*pos+1,mid+1,right,i,j);
return max(query(2*pos,left,mid,i,j),query(2*pos+1,mid+1,right,i,j));
}
void intervals(int ini,int fim){
//printf("Intervals %d %d\n",ini,fim);
if(ini > fim) return;
int maioral = query(1,1,N,ini,fim).second;
esq[maioral] = ini;
dir[maioral] = fim;
intervals(ini,maioral-1);
intervals(maioral+1,fim);
}
void build2(int pos,int left,int right){
//printf("Build2 %d %d %d\n",pos,left,right);
if(left == right){
seg[pos] = ii(dp[left],left);
return;
}
int mid = (left+right)/2;
build2(2*pos,left,mid);
build2(2*pos+1,mid+1,right);
seg[pos] = min(seg[2*pos],seg[2*pos+1]);
}
ii query2(int pos,int left,int right,int i,int j){
//printf("Query2 %d %d %d %d\n",pos,left,right,i,j);
if(left >= i && right <= j) return seg[pos];
int mid = (left+right)/2;
if(j <= mid) return query2(2*pos,left,mid,i,j);
else if(i >= mid + 1) return query2(2*pos+1,mid+1,right,i,j);
return min(query2(2*pos,left,mid,i,j),query2(2*pos+1,mid+1,right,i,j));
}
void solve(int k){
for(int i = 0;i<k-1;i++) dp[i] = INF;
vector<ii> sweep;
multiset<int> valores;
build2(1,1,N);
for(int i = 1;i<=N;i++){
int lo = max(esq[i] - 1,1);
int hi = i - 1;
if(lo > hi || max(lo,hi) <= 0) continue;
int novo = query2(1,1,N,lo,hi).first + vetor[i];
sweep.push_back(ii(i,novo));
sweep.push_back(ii(dir[i] + 1,-novo));
}
//printf("Ok1\n");
sort(sweep.begin(),sweep.end());
int ptr = 0;
for(int i = k;i<=N;i++){
//printf("Sweep\n");
while(ptr < sweep.size() && sweep[ptr].first <= i){
//printf("While\n");
int val = sweep[ptr].second;
if(val > 0){
valores.insert(val);
}
else{
valores.erase(valores.find(-val));
}
ptr++;
}
if(valores.empty()){
dp[i] = INF;
}
else{
dp[i] = (*valores.begin());
}
}
//printf("Ok2\n");
sweep.clear();
valores.clear();
}
int main(){
scanf("%d %d",&N,&K);
for(int i = 1;i<=N;i++){
scanf("%d",&vetor[i]);
dp[i] = vetor[i];
}
build(1,1,N);
intervals(1,N);
for(int i = 1;i<=N;i++) dp[i] = query(1,1,N,1,i).first;
for(int k = 2;k<=K;k++) solve(k);
printf("%d\n",dp[N]);
return 0;
}
Compilation message
blocks.cpp: In function 'void solve(int)':
blocks.cpp:74:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ptr < sweep.size() && sweep[ptr].first <= i){
^
blocks.cpp: In function 'int main()':
blocks.cpp:97:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&K);
^
blocks.cpp:99:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&vetor[i]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
440 KB |
Output is correct |
5 |
Correct |
2 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
472 KB |
Output is correct |
7 |
Correct |
2 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
528 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
616 KB |
Output is correct |
13 |
Correct |
2 ms |
616 KB |
Output is correct |
14 |
Correct |
1 ms |
616 KB |
Output is correct |
15 |
Correct |
1 ms |
616 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
732 KB |
Output is correct |
6 |
Correct |
2 ms |
732 KB |
Output is correct |
7 |
Correct |
2 ms |
732 KB |
Output is correct |
8 |
Correct |
1 ms |
732 KB |
Output is correct |
9 |
Correct |
1 ms |
732 KB |
Output is correct |
10 |
Correct |
2 ms |
732 KB |
Output is correct |
11 |
Correct |
1 ms |
732 KB |
Output is correct |
12 |
Correct |
1 ms |
732 KB |
Output is correct |
13 |
Correct |
2 ms |
732 KB |
Output is correct |
14 |
Correct |
1 ms |
732 KB |
Output is correct |
15 |
Correct |
1 ms |
732 KB |
Output is correct |
16 |
Correct |
1 ms |
732 KB |
Output is correct |
17 |
Correct |
1 ms |
732 KB |
Output is correct |
18 |
Correct |
1 ms |
732 KB |
Output is correct |
19 |
Correct |
2 ms |
732 KB |
Output is correct |
20 |
Correct |
2 ms |
732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
732 KB |
Output is correct |
2 |
Correct |
1 ms |
732 KB |
Output is correct |
3 |
Correct |
1 ms |
732 KB |
Output is correct |
4 |
Correct |
1 ms |
732 KB |
Output is correct |
5 |
Correct |
1 ms |
732 KB |
Output is correct |
6 |
Correct |
1 ms |
732 KB |
Output is correct |
7 |
Correct |
1 ms |
732 KB |
Output is correct |
8 |
Correct |
1 ms |
732 KB |
Output is correct |
9 |
Correct |
1 ms |
732 KB |
Output is correct |
10 |
Correct |
1 ms |
732 KB |
Output is correct |
11 |
Correct |
2 ms |
732 KB |
Output is correct |
12 |
Correct |
1 ms |
732 KB |
Output is correct |
13 |
Correct |
2 ms |
732 KB |
Output is correct |
14 |
Correct |
2 ms |
732 KB |
Output is correct |
15 |
Correct |
3 ms |
732 KB |
Output is correct |
16 |
Correct |
4 ms |
732 KB |
Output is correct |
17 |
Correct |
3 ms |
732 KB |
Output is correct |
18 |
Correct |
3 ms |
732 KB |
Output is correct |
19 |
Correct |
3 ms |
732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
1660 KB |
Output is correct |
2 |
Correct |
28 ms |
2924 KB |
Output is correct |
3 |
Correct |
126 ms |
4664 KB |
Output is correct |
4 |
Execution timed out |
1076 ms |
4964 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |