Codeforces-1077F:Pictures with Kittens(DP+单调队列优化)

F2. Pictures with Kittens (hard version)
time limit per test 2.5 seconds
memory limit per test 512 megabytes
inputstandard input
outputstandard output

The only difference between easy and hard versions is the constraints.

Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of n consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the i-th picture has beauty ai.

Vova wants to repost exactly x pictures in such a way that:

each segment of the news feed of at least k consecutive pictures has at least one picture reposted by Vova;
the sum of beauty values of reposted pictures is maximum possible.
For example, if k=1 then Vova has to repost all the pictures in the news feed. If k=2 then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them.

Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions.

Input
The first line of the input contains three integers n , k n,k n,k and x ( 1 ≤ k , x ≤ n ≤ 5000 ) x (1≤k,x≤n≤5000) x(1k,xn5000) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost.

The second line of the input contains n integers a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 1 0 9 ) a_1,a_2,…,a_n (1≤a_i≤10^9) a1,a2,,an(1ai109), where ai is the beauty of the i-th picture.

Output
Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement.

Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement.

Examples
input
5 2 3
5 1 3 10 1
output
18
input
6 1 5
10 30 30 70 10 10
output
-1
input
4 3 1
1 100 1 1
output
100

思路:DP。 d [ i ] [ j ] d[i][j] d[i][j]表示前 i i i个数中,在选了 a i a_i ai的情况下,总共选择了 j j j个数的最大值。

则有 d [ i ] [ j ] = m a x ( d [ y ] [ j − 1 ] + a [ i ] ) , ( i − k ≤ y ) d[i][j]=max(d[y][j-1]+a[i]),(i-k\le y) d[i][j]=max(d[y][j1]+a[i]),(iky)

最后的答案 = m a x ( d [ i ] [ x ] ) , ( n − k + 1 ≤ i ) =max(d[i][x]),(n-k+1\le i) =max(d[i][x])(nk+1i)

其中可以发现转移的部分: d [ i ] [ j ] = m a x ( d [ y ] [ j − 1 ] + a [ i ] ) , ( i − k ≤ y ) d[i][j]=max(d[y][j-1]+a[i]),(i-k\le y) d[i][j]=max(d[y][j1]+a[i]),(iky)

是求 y y y在区间 [ i − k , i − 1 ] [i-k,i-1] [ik,i1]内的 d [ y ] [ j − 1 ] d[y][j-1] d[y][j1]的最大值,于是可以构造一个单调递减的队列优化DP。

PS:用线段树写MLE了,我的线段树太丑了。

#include<bits/stdc++.h>
using namespace std;
const int MAX=5e3+10;
typedef long long ll;
deque<pair<ll,ll> >p[MAX];
ll a[MAX];
int main()
{
    int n,k,m;
    cin>>n>>k>>m;
    for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
    for(int i=1;i<=n;i++)
    {
        for(int j=m;j>=2;j--)
        {
            if(p[j-1].empty())continue;
            pair<ll,ll>last=p[j-1].back();
            while(!p[j].empty()&&last.second+a[i]>=p[j].front().second)p[j].pop_front();
            last.second+=a[i];
            last.first=i;
            p[j].push_front(last);
        }
        if(i<=k)
        {
            while(!p[1].empty()&&a[i]>=p[1].front().second)p[1].pop_front();
            p[1].push_front({i,a[i]});
        }
        for(int j=1;j<=m;j++)
        {
            while(!p[j].empty())
            {
                pair<ll,ll>last=p[j].back();
                if(last.first+k<i+1)p[j].pop_back();
                else break;
            }

        }
    }
    if(p[m].empty())puts("-1");
    else cout<<p[m].back().second<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值