[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 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)的更多相关文章
- 2018.12.29 codeforces 940E. Cashback(线性dp)
传送门 题意:给出一个nnn个数的序列,要求将序列分成若干段,对于一段长度为kkk的自动删去最小的⌊kc⌋\left \lfloor \frac{k}{c} \right \rfloor⌊ck⌋个数 ...
- ACdreamOJ 1154 Lowbit Sum (数字dp)
ACdreamOJ 1154 Lowbit Sum (数位dp) ACM 题目地址:pid=1154" target="_blank" style="color ...
- 「SDOI2016」储能表(数位dp)
「SDOI2016」储能表(数位dp) 神仙数位 \(dp\) 系列 可能我做题做得少 \(QAQ\) \(f[i][0/1][0/1][0/1]\) 表示第 \(i\) 位 \(n\) 是否到达上界 ...
- 【HDU1693】Eat the Trees(插头dp)
[HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...
- 【BZOJ1814】Ural 1519 Formula 1 (插头dp)
[BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...
- 【BZOJ4712】洪水(动态dp)
[BZOJ4712]洪水(动态dp) 题面 BZOJ 然而是权限题QwQ,所以粘过来算了. Description 小A走到一个山脚下,准备给自己造一个小屋.这时候,小A的朋友(op,又叫管理员)打开 ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- 【POJ2411】Mondriaan's Dream(轮廓线DP)
[POJ2411]Mondriaan's Dream(轮廓线DP) 题面 Vjudge 题解 这题我会大力状压!!! 时间复杂度大概是\(O(2^{2n}n^2)\),设\(f[i][S]\)表示当前 ...
- 能量项链 (区间DP)
能量项链 (区间DP) 问题引入 能量项链 洛谷 P1063 思路 诸如此类不能线性规划的问题要用到区间DP,区间DP一般就是三层循环,第一层表示区间长度(本题即\(n\)),第二层枚举起点并根据第一 ...
随机推荐
- 3.HTML DOM
介绍 DOM(Document Object Model)文档对象模型,定义了访问 HTML 和 XML 文档的标准,它允许程序和脚本动态地访问和更新文档的内容.结构和样式. 那显然,HTML DOM ...
- 微信小程序电商实战-入门篇
小程序开发工具有新版本更新啦!开发体验更好了,详情可以查看微信公众平台-小程序https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.h ...
- java分页三个类 PageBean ResponseUtil StringUtil
package ssmy.page; /** * 分页类 * @author Jesse * */public class PageBean { private int page;//第几页 priv ...
- mongdb增加字段和删除字段
增加字段 db.xxx.update({},{"$set":{"column1":1,"column2":0}},false,true); ...
- 概念:详细讲解url和路由概念
例如:一个网址为 http://www.abc.com/aa 定义:/aa = bb/cc/dd 那么:http://www.abc.com/aa就是一个url,那么我们可以得出:网址=url 而当我 ...
- GitLab-Runner 安装配置
https://docs.gitlab.com/runner/install/linux-repository.html 直接看官方教程 systemctl status gitlab-runner. ...
- IIS https绑定主机头方法 [可实现禁止直接通过IP访问及“IIS中绑定多个https(但需要多个IP地址)”]
步骤: 打开:%systemroot%\SYSTEM32\inetsrv\config\ 打开:applicationHost.config 查找:<binding protocol=" ...
- 同步软件UltraCompare 64位 软件及注册机
软件及注册机下载: https://share.weiyun.com/f09e6243887e374ead1b3a3ab8f611a9 软件官方下载地址: https://www.ultraedit ...
- java——栈和队列 面试题
(1)栈的创建 (2)队列的创建 (3)两个栈实现一个队列 (4)两个队列实现一个栈 (5)设计含最小函数min()的栈,要求min.push.pop.的时间复杂度都是O(1) (6)判断栈的push ...
- 1.10 从表中随机返回n条记录
同时使用内置函数的rand函数. limit 和order by: select * from emp order by rand() limit 2;