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 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;
}

[CodeForces940E]Cashback(set+DP)的更多相关文章

  1. 2018.12.29 codeforces 940E. Cashback(线性dp)

    传送门 题意:给出一个nnn个数的序列,要求将序列分成若干段,对于一段长度为kkk的自动删去最小的⌊kc⌋\left \lfloor \frac{k}{c} \right \rfloor⌊ck​⌋个数 ...

  2. ACdreamOJ 1154 Lowbit Sum (数字dp)

    ACdreamOJ 1154 Lowbit Sum (数位dp) ACM 题目地址:pid=1154" target="_blank" style="color ...

  3. 「SDOI2016」储能表(数位dp)

    「SDOI2016」储能表(数位dp) 神仙数位 \(dp\) 系列 可能我做题做得少 \(QAQ\) \(f[i][0/1][0/1][0/1]\) 表示第 \(i\) 位 \(n\) 是否到达上界 ...

  4. 【HDU1693】Eat the Trees(插头dp)

    [HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...

  5. 【BZOJ1814】Ural 1519 Formula 1 (插头dp)

    [BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...

  6. 【BZOJ4712】洪水(动态dp)

    [BZOJ4712]洪水(动态dp) 题面 BZOJ 然而是权限题QwQ,所以粘过来算了. Description 小A走到一个山脚下,准备给自己造一个小屋.这时候,小A的朋友(op,又叫管理员)打开 ...

  7. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  8. 【POJ2411】Mondriaan's Dream(轮廓线DP)

    [POJ2411]Mondriaan's Dream(轮廓线DP) 题面 Vjudge 题解 这题我会大力状压!!! 时间复杂度大概是\(O(2^{2n}n^2)\),设\(f[i][S]\)表示当前 ...

  9. 能量项链 (区间DP)

    能量项链 (区间DP) 问题引入 能量项链 洛谷 P1063 思路 诸如此类不能线性规划的问题要用到区间DP,区间DP一般就是三层循环,第一层表示区间长度(本题即\(n\)),第二层枚举起点并根据第一 ...

随机推荐

  1. sql分组数据去重

    #分组获得每个机柜里服务器占用的机架总数,如552807e6-b428-4184-b219-ae368c68ddb3占用4个 mysql> select cabinet_uuid, count( ...

  2. yii网站未来改进

    1.去掉debug模式 2.下载文件复选 3.文章发布.评论.赞.标签等系统

  3. 翻译-ExcelDNA开发文档-首页

    转载自个人主页 前言 ExcelDNA是一名国际友人开发的开源框架,文档全是英文文档,当时看的时候非常吃力,现在将英文文档翻译过来,为的是让自己加深印象以及自己以后看的时候能不用这么吃力. 介绍 Ex ...

  4. rosservice call ERROR:Unable to load type ... Have you typed 'make'

    you need to source in the new terminal $ source ~/catkin_ws/devel/setup.bash

  5. Metasploitable渗透测试实战——生成木马

    攻击机:kali 目标机:windows 1.生成木马  wincap发送至本机 2.进入msf  (命令:msfconsole)启动监听 3.当目标点击test.exe(可伪装)时,触发后门,实现入 ...

  6. SAP标准培训课程C4C10学习笔记(四)第四单元

    这个单元的内容是产品主数据和Price list. Hierarchy UI上按钮New的enable/disable逻辑 SAP CRM和C4C数据同步的两种方式概述:SAP PI和HCI 一种轻量 ...

  7. 859. Buddy Strings (wrong 4 times so many cases to test and consider) if else**

    Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...

  8. nutzwk运行后wk-web中生成ehcache.disk.store.dir有什么用,怎么去掉

    nutzwk运行后wk-web中生成ehcache.disk.store.dir有什么用,怎么去掉  发布于 29天前  作者 qq_96c46988  64 次浏览  复制  上一个帖子  下一个帖 ...

  9. 【LOJ6029】「雅礼集训 2017 Day1」市场(线段树裸题)

    点此看题面 大致题意: 维护序列,支持区间加法,区间除法(向下取整),区间求\(min\)和区间求和. 线段树维护区间除法 区间加法.区间求\(min\)和区间求和都是线段树基本操作,因此略过不提. ...

  10. Heterogeneity Activity Recognition Data Set类别

    Heterogeneity Activity Recognition Data Set:https://archive.ics.uci.edu/ml/datasets/Heterogeneity+Ac ...