[CodeForces940E]Cashback(set+DP)

Description

Since you are the best Wraith King, Nizhniy Magazin «Mir» at the centre of Vinnytsia is offering you a discount.

You are given an array a of length n and an integer c.

The value of some array b of length k is the sum of its elements except for the 9f62d3be7333102b6a6fc4af62b05cfa620cccee.png smallest. For example, the value of the array [3, 1, 6, 5, 2] with c = 2 is 3 + 6 + 5 = 14.

Among all possible partitions of a into contiguous subarrays output the smallest possible sum of the values of these subarrays.

Solution

这里有一个贪心的思想,就是只需要划分长度为1或者长度为c的区间,其他的不会比这两种更优

首先裸的DP,\(dp[i]=min\{dp[j]+Cost_{j+1,i}\}\)

然后只要对长度为c的区间进行更新,可以用multiset进行存储数据

(STL中的multiset与set的区别在于可以存储相同的元素)

具体见代码

Code

#include <cstdio>
#include <algorithm>
#include <set>
#define ll long long
#define N 100010
using namespace std;

int n,c;
ll A[N],dp[N],sum;
multiset<ll> q;//默认升序排列

int main(){
    scanf("%d%d",&n,&c);
    for(int i=1;i<=n;i++)scanf("%I64d",&A[i]);
    for(int i=1;i<=n;i++){
        sum+=A[i];
        dp[i]=dp[i-1]+A[i];
        q.insert(A[i]);
        if(i>c){q.erase(q.find(A[i-c]));sum-=A[i-c];}//删除最小的元素
        if(i>=c){dp[i]=min(dp[i],dp[i-c]+sum-*q.begin());}//转移
    }
    printf("%I64d\n",dp[n]);
    return 0;
}

转载于:https://www.cnblogs.com/void-f/p/8476126.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值