codeforces1077F2 Pictures with Kittens (优先队列+DP)

探讨了在限定条件下,如何通过优化动态规划算法,使用优先队列提高效率,解决每隔m个元素必须选取至少一个,且总共需选取k个元素以最大化总价值的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                                     F2. Pictures with Kittens (hard version)

time limit per test

2.5 seconds

memory limit per test

512 megabytes

input

standard input

output

standard 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 nn consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the ii-th picture has beauty aiai.

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

  • each segment of the news feed of at least kk 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=1k=1 then Vova has to repost all the pictures in the news feed. If k=2k=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,kn,k and xx (1≤k,x≤n≤50001≤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 nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the beauty of the ii-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

Copy

5 2 3
5 1 3 10 1

output

Copy

18

input

Copy

6 1 5
10 30 30 70 10 10

output

Copy

-1

input

Copy

4 3 1
1 100 1 1

output

Copy

100

 

一、原题地址

点我传送

 

二、大致题意

n个物体,每隔m个物体必须取一个拿走,需要取走k个。

询问最大能得到的价值是多少。

 

三、大致思路

很容易想到n^3的方法,

(t=max(i-m,0);t<i;t++)  dp[i][j]=max(dp[i][j],dp[t][j-1]+a[i]);    dp[ i ][ j ]表示更新到第i个物品已经取走了j个的情况

但是在增强的数据里面显然不行。

观察到dp[t][j-1]的更新实际上是可以用优先队列来完成的,因为dp[ i ][ j ]显然是只需要取到dp[ t ][ j-1 ]中最大的一个就可以了,那么我们就先把取走了 j 的信息先用优先队列储存好就可以了。随着 i 的更新,每次取出队首元素时来考察他们的位置是否符合。

最后由于元素太多了,第一次交T掉了。改了个continue上去,跑了2400ms过了非常神奇。

 

四、代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;


struct Node
{
    LL val;
    int pos,id;
    bool operator < (const Node b)const
    {
        if(val!=b.val)return val<b.val;
        else
        {
            if(pos!=b.pos) return pos<b.pos;
            else
            {
                return id<b.id;
            }
        }
    }
};
LL dp[5005][5005];
LL a[5005];
int n,m,k;
priority_queue<Node>q[5005];
int main()
{
    int tot=0;
    scanf("%d %d %d",&n,&m,&k);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    memset(dp,-inf,sizeof(dp));
    dp[0][0]=0;
    Node st;
    st.id=tot++;st.val=0;st.pos=0;
    q[0].push(st);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=k;j++)
        {
            if(j>i)break;
            while(!q[j-1].empty()&&q[j-1].top().pos<i-m)q[j-1].pop();
            if(q[j-1].size()==0) continue;
            Node t=q[j-1].top();
            t.id=tot++;
            t.val=dp[t.pos][j-1]+a[i];
            t.pos=i;
            dp[i][j]=max(dp[i][j],t.val);
        }
        for(int j=1;j<=k;j++)
        {
            if(dp[i][j]<=0)continue;//这里不加常数很大,第一次TLE的位置
            Node t;
            t.id=tot++;
            t.val=dp[i][j];
            t.pos=i;
            q[j].push(t);
        }
    }
    LL ans=-1;
    for(int i=n;i>=n-m+1;i--) ans=max(ans,dp[i][k]);
    printf("%lld\n",ans);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值