Codeforces Round #466 (Div. 2) E. Cashback
Codeforces Round #466 (Div. 2) E. Cashback(dp + 贪心)
题意:
给一个长度为\(n\)的序列\(a_i\),给出一个整数\(c\)
定义序列中一段长度为k的区间的贡献为区间和减去前\(\lfloor \frac{k}{c} \rfloor\)小数的和
现在要给序列\(a_i\)做一个划分,使得贡献的总和最小。
思路:
容易想到最裸的dp的思路
\(dp[i] = min(dp[j] + cost(j,i)) , j < i\), \(cost(j,i)\)就是区间[j,i]的贡献
这样暴力枚举复杂度是\(O(n ^ {2} log n)\)的
观察发现 划分成一段\(k * c\)的区间不会优于划分成\(k\)段长度为\(c\)的区间
同理分成非\(c\)的整数倍长度的区间是不会优于拆分成\(c\)的整数倍和一段长度小于\(c\)的区间
由于长度小于\(c\)的区间是没有贡献的,所以和拆成长度为1的区间是等价的。
所以最优的划分方式就是分成长度为1或者分成长度为c,再来做dp就可以了。
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 1e5 + 10;
const int inf = 0x3f3f3f3f;
int a[N];
int n, c;
int mi[N << 2];
LL dp[N],sum[N];
void build(int l,int r,int rt){
if(l == r){
scanf("%d",a + l);
mi[rt] = a[l];
return ;
}
int m = l + r >> 1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
mi[rt] = min(mi[rt<<1],mi[rt<<1|1]);
}
int querymi(int L,int R,int l,int r,int rt){
if(L <= l && R >= r) return mi[rt];
int ans = inf;
int m = l + r >> 1;
if(L <= m) ans = min(ans, querymi(L,R,l,m,rt<<1));
if(R > m) ans = min(ans,querymi(L,R,m+1,r,rt<<1|1));
return ans;
}
int main(){
scanf("%d%d",&n,&c);
build(1,n,1);
for(int i = 1;i <= n;i++) sum[i] = sum[i - 1] + a[i];
for(int i = 1;i <= n;i++){
dp[i] = dp[i - 1] + a[i];
if(i >= c){
dp[i] = min(dp[i], dp[i - c] + sum[i] - sum[i - c] - querymi(i - c + 1,i,1,n,1));
}
}
cout<<dp[n]<<endl;
return 0;
}
Codeforces Round #466 (Div. 2) E. Cashback的更多相关文章
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #466 (Div. 2) Solution
从这里开始 题目列表 小结 Problem A Points on the line Problem B Our Tanya is Crying Out Loud Problem C Phone Nu ...
- Codeforces Round #466 (Div. 2)
所有的题目都可以在CodeForces上查看 中间看起来有很多场比赛我没有写了 其实是因为有题目没改完 因为我不想改,所以就没有写了(大部分题目还是改完了的) 我还是觉得如果是打了的比赛就一场一场写比 ...
- Codeforces Round #466 (Div. 2) -A. Points on the line
2018-02-25 http://codeforces.com/contest/940/problem/A A. Points on the line time limit per test 1 s ...
- Codeforces Round #466 (Div. 2) 题解
人生中第三次\(CF\)... 考试中切了\(A\)~\(E\) \(F\)题会做没时间写 题解 A:Points on the line 题意 给定一个数列,删最小的数,使最大差不大于一个定值 So ...
- Codeforces Round #466 (Div. 2) A. Points on the line[数轴上有n个点,问最少去掉多少个点才能使剩下的点的最大距离为不超过k。]
A. Points on the line time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #466 (Div. 2) B. Our Tanya is Crying Out Loud[将n变为1,有两种方式,求最小花费/贪心]
B. Our Tanya is Crying Out Loud time limit per test 1 second memory limit per test 256 megabytes inp ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- vcf-tools 笔记
vcf-query: 通过 vcf-query 提取DP (reads depth). ~/zengs/Tools/vcftools/perl/vcf-query -f '%CHROM\t%POS\t ...
- 关于 Windows 10 字体安装目录的问题
不知从什么时候开始,本人台式机的Win10系统在安装字体的时候并不是安装到C:\Windows\Fonts目录中,而是安装到%USERPROFILE%\AppData\Local\Microsoft\ ...
- 『Golang』Go简介以及环境搭建
简介 go语言是由Google进行维护的一个编程语言,发布自2009年.其以良好的编程风格.优秀的并发机制被广大的技术人员所接受. 使用go语言开发的优秀的产品: Docker gocode lime ...
- Java: Replace a string from multiple replaced strings to multiple substitutes
Provide helper methods to replace a string from multiple replaced strings to multiple substitutes im ...
- 「日常训练」Regular Bridge(Codeforces Round 306 Div.2 D)
题意与分析 图论基础+思维题. 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back #defi ...
- 「知识学习」二分图的最大匹配、完美匹配和匈牙利算法(HDU-2063)
定义 如果一个图\((E,V)\)的顶点集\(E\)能够被能够被分成两个不相交的集合\(X,Y\),且每一条边都恰连接\(X,Y\)中的各一个顶点,那么这个图就是一个二分图. 容易得知,它就是不含有奇 ...
- android自动化のadb常用命令(不定期更新)
1. adb devices 执行结果是adb为每一个设备输出以下状态信息:序列号(serialNumber) — 由adb创建的使用控制台端口号的用于唯一标识一个模拟器或手机设备的字符串,格式是 & ...
- HashMap 阅读
最近研究了一下java中比较常见的map类型,主要有HashMap,HashTable,LinkedHashMap和concurrentHashMap.这几种map有各自的特性和适用场景.使用方法的话 ...
- 【MFC】学习与问题整合
需要源码联系邮件:kangxlchn@163.com 1.新建一个MFC工程(基于对话框) 环境:vs2017 统统NEXT 新建完成后打开MFCPrj.cpp文件 打开类试图 每创建一个MFC项目, ...
- 最小生成树(II)与Kruskal算法
为防止网页加载过慢,故分两章.上接https://www.cnblogs.com/Uninstalllingyi/p/10479470.html Kruskal算法——将森林合并成树 玩过瘟疫公司吗… ...