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 ...
随机推荐
- python 水仙花
#简单def narcissus(): for n in range(100, 1000, 1): a, b, c = n//100, (n//10)%10, (n%100)%10 if a ** 3 ...
- Objective-C NSString基本使用 类方法 self关键字
NSString基本使用 #import <Foundation/Foundation.h> int main() { //最简单的创建字符串的方式 NSString *str = @&q ...
- Siki_Unity_2-10_数据结构与算法
Unity 2-10 数据结构与算法 任务1-1:数据结构简介 数据结构:数据存储的结构,数据之间的关系 数据结构分类: 集合:同属于一个集合 线性结构:数据元素存在一对一的关系 树形结构:数据元素存 ...
- Spring Cloud(六):Hystrix 监控数据聚合 Turbine【Finchley 版】
Spring Cloud(六):Hystrix 监控数据聚合 Turbine[Finchley 版] 发表于 2018-04-17 | 更新于 2018-05-07 | 上一篇我们介绍了使用 H ...
- 单元测试模块unittest使用学习
工作原理: unittest中最核心的四个概念是:test case, test suite, test runner, test fixture. 一个TestCase的实例就是一个测试用例.什么是 ...
- @Configuration和@Bean
@Configuration可理解为用spring的时候xml里面的标签 @Bean可理解为用spring的时候xml里面的标签 Spring Boot不是spring的加强版,所以@Configur ...
- hive创建外部表
Create [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment], ...)] ...
- Python高级编程-序列化
在程序运行的过程中,所有的变量都是在内存中,比如,定义一个dict: dict1 = {'name': 'Rob', 'age': 19, 'score': 90} 可以随时修改变量,比如把age改成 ...
- 【备忘】mysql常用操作汇总
1.增删改查 // 插入一条数据 insert into tableName values('liu','bei') // 删除一条数据 delete from tableName where las ...
- js学习之正则表达式
js学习之正则表达式 正则表达式(英语:Regular Expression,在代码中常简写为regex.regexp或RE)使用单个字符串来描述.匹配一系列符合某个句法规则的字符串搜索模式 一:语法 ...