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

版权声明:http://blog.csdn.net/Mitsuha_。 https://blog.csdn.net/Mitsuha_/article/details/84189415

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 and x ( 1 k , x n 5000 ) x (1≤k,x≤n≤5000) — 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) , 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] 表示前 i i 个数中,在选了 a i a_i 的情况下,总共选择了 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)

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

其中可以发现转移的部分: 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)

是求 y y 在区间 [ i k , i 1 ] [i-k,i-1] 内的 d [ y ] [ j 1 ] d[y][j-1] 的最大值,于是可以构造一个单调递减的队列优化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;
}

猜你喜欢

转载自blog.csdn.net/Mitsuha_/article/details/84189415