P3287 [SCOI2014]方伯伯的玉米田

P3287 [SCOI2014]方伯伯的玉米田

链接:https://www.luogu.org/problemnew/show/P3287

题目描述

方伯伯在自己的农田边散步,他突然发现田里的一排玉米非常的不美。这排玉米一共有N株,它们的高度参差不齐。方伯伯认为单调不下降序列很美,所以他决定先把一些玉米拔高,再把破坏美感的玉米拔除掉,使得剩下的玉米的高度构成一个单调不下降序列。方伯伯可以选择一个区间,把这个区间的玉米全部拔高1单位高度,他可以进行最多K次这样的操作。拔玉米则可以随意选择一个集合的玉米拔掉。问能最多剩多少株玉米,来构成一排美丽的玉米。

输入输出格式

输入格式:

 

第1行包含2个整数n,K,分别表示这排玉米的数目以及最多可进行多少次操作。第2行包含n个整数,第i个数表示这排玉米,从左到右第i株玉米的高度ai。

 

输出格式:

 

输出1个整数,最多剩下的玉米数。

 

输入输出样例

输入样例#1: 复制
3 1
2 1 3
输出样例#1: 复制
3

说明

1 < N < 10000,1 < K <= 500,1 <= ai <=5000

题解:dp[i][p]表示处理到第i个玉米,增加了p的高度的最多剩余玉米;

dp[i][p] = max(dp[j][k]) + 1 (a[j] + k <= a[i]+p && k <= p);

这个就可以用一个树状树组维护;

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;
const int M = 6000 + 5;
int c[505][M], dp[M][505], a[100005];
int query(int x, int y){
    int ans = 0;
    for(; x; x -= x&(-x))
        for(int t = y; t; t -= t&(-t))
            ans = max(ans, c[x][t]);
    return ans;
}
void add(int x, int y, int val){
    for(; x <= 501 ; x += x&(-x))
        for(int t = y; t < M; t += t&(-t))
            c[x][t] = max(c[x][t], val);
}

int main(){
    int n, k, ans = 0;
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)scanf("%d", a + i);
    for(int i = 1; i <= n; i++){
        for(int p = k; p >= 0; p--){
            dp[a[i] + p][p] = query(p + 1, a[i]+p) + 1;
            ans = max(ans, dp[a[i] + p][p]);
            add(p + 1, a[i]+p, dp[a[i] + p][p]);
        //    printf("%d %d\n", a[i]+p, dp[i]);
        }
    }
    printf("%d\n", ans);
}
View Code

 

posted @ 2018-10-30 17:35  Ed_Sheeran  阅读(202)  评论(0编辑  收藏  举报