写在前面

简单的单调队列优化 DP

处理略微有点恶心,于是乎,用来取 \(\max\) 的极小值直接开到了 long long 的最小极限,了 define int long long /cy

算法思路

必须按编号顺序加材料,明显的阶段性,且数据范围明显地提示我们可以 DP

状态也很好想,设 \(f_{i, j}\) 表示放完前 \(i\) 个物品后锅内有 \(j\) 个物品时的最大答案。

那么使用填表法转移:

\[f_{i, j} = \max_{j - 1 \le k \le j + s - 1}\{f_{i - 1,k}\} + j \times a_i
\]

那么发现 \(k\) 的取值范围随着 \(j\) 的变化刚好是个滑动窗口,其余的项都是输入时或枚举过程中的定值,因此使用单调队列优化取最大值的操作。

另外表示阶段的 \(i\) 只会取到上一个阶段的答案,因此开滚动数组压掉第一维。

Tips

建议把可能需要开 long long 的都打开,如果不觉得很傻或者比较懒的话也可以直接 define int long long。

内层循环可以倒序枚举,这样就只需要一开始的时候往单调队列里压一个元素。不用乱七八糟的处理。

初始化极小值的时候要足够小亲测 \(-10^{12}\) 都不够用,还不能在加上一些负值之后爆 long long 的最小范围。

Code

/*

By chen_green

2020/11/5

设 f[i][j]表示放完前 i 件物品后锅中已经放了 j 件物品的最大耐久度

f[i][j] = max{f[i - 1][k]} + j * a[i] (j - 1 <= k <= j - 1 + s)

滚动数组 + 单调队列优化

*/

#include <bits/stdc++.h>
#define int long long #define LL long long using namespace std; inline int read0() {
int fh = 1, w = 0; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
for (; isdigit(ch); ch = getchar()) w = (w << 3) + (w << 1) + (ch ^ '0');
return fh * w;
} inline LL read() {
LL fh = 1, w = 0; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
for (; isdigit(ch); ch = getchar()) w = (w << 3) + (w << 1) + (ch ^ '0');
return fh * w;
} const int Maxn = 5505; LL f[2][Maxn]; LL a[Maxn]; int n, w, s; deque<LL> dq; void initdq() {while(!dq.empty()) dq.pop_back();}
void push(int x) {
if((int)dq.size() >= (int)(s + 1)) dq.pop_front();
while((!dq.empty()) && (dq.back() <= x)) dq.pop_back();
dq.push_back(x);
}
LL Getmax() {
return dq.front();
} signed main() {
n = read0(); w = read0(); s = read0();
for(register int i = 1; i <= n; ++i) {
a[i] = read();
}
for(register int i = 0; i <= w; ++i) f[0][i] = f[1][i] = -9223372036854775808 / 2;
LL f0 = f[0][0];
f[0][0] = 0;
LL ans = -9223372036854775808;
for(register int i = 1; i <= n; ++i) {
if(i == 2) f[0][0] = f0;
initdq();
push(f[i - 1 & 1][w]);
for(register int j = w; j >= 1; --j) {
push(f[i - 1 & 1][j - 1]);
f[i & 1][j] = Getmax() + j * a[i];
//cout << f[i & 1][j] << " ";
}
}
for(int i = 1; i <= w; ++i) {
ans = max(ans, f[n & 1][i]);
}
printf("%lld", ans);
}

P5858 Golden Swold的更多相关文章

  1. P5858 「SWTR-03」Golden Sword

    题面: Link 题面有点长,不想粘了,QAQ. 题解: 一句话题意,你有 \(n\) 件物品需要依次放进去,每个物品放进去之后会得到一定的权值,为当前锅炉里面的物品的数量乘以 \(a_i\) 每次在 ...

  2. Why The Golden Age Of Machine Learning is Just Beginning

    Why The Golden Age Of Machine Learning is Just Beginning Even though the buzz around neural networks ...

  3. C Golden gun的巧克力

    Time Limit:1000MS  Memory Limit:65535K 题型: 编程题   语言: 无限制 描述 众所周知,13级有尊大神Golden gun,人称根叔,简称金枪!众立志进校队的 ...

  4. 10 Golden Rules of Project Risk Management

    The benefits of risk management in projects are huge. You can gain a lot of money if you deal with u ...

  5. The golden ratio: 1.618

    http://www.chinaz.com/design/2015/1109/467968_2.shtml The golden ratio: 1.618 a/b=b/(a+b) The Fibona ...

  6. 【UVA 11383】 Golden Tiger Claw (KM算法副产物)

    Omi, Raymondo, Clay and Kimiko are on new adventure- in search of new Shen Gong Wu. But EvilBoy Geni ...

  7. Golden Pyramid

    Golden Pyramid Our Robo-Trio need to train for future journeys and treasure hunts. Stephan has built ...

  8. Oracle Golden Gate - 概念和机制 (ogg)

    Golden Gate(简称OGG)提供异构环境下交易数据的实时捕捉.变换.投递. OGG支持的异构环境有: OGG的特性: 对生产系统影响小:实时读取交易日志,以低资源占用实现大交易量数据实时复制 ...

  9. 转://Oracle Golden Gate 概念和原理

    引言:Oracle Golden Gate是Oracle旗下一款支持异构平台之间高级复制技术,是Oracle力推一种HA高可用产品,简称“OGG”,可以实现Active-Active 双业务中心架构 ...

随机推荐

  1. easyui中给table列表中加序号

    $('#xyData_healthList').datagrid({ width: 'auto', height: 'auto', striped: true, fit: true, paginati ...

  2. 浅入kubernetes(1):Kubernetes 入门基础

    目录 Kubernetes 入门基础 Introduction basic of kubernetes What Is Kubernetes? Components of Kubernetes Kub ...

  3. 图解 Java 数据结构

    图解Java数据结构: 一.链表       Java ListNode     https://www.cnblogs.com/easyidea/p/13371863.html 二.栈       ...

  4. 微信小程序 - bilibili模仿

    今天真是个大坑,onLoad => 写成了OnLoad 程序一直没法执行, 晚上下班的时候,在微信的小程序中,this不可以直接访问data中的值,得this.data才能访问..

  5. 图解SparkStreaming与Kafka的整合,这些细节大家要注意!

    前言 老刘是一名即将找工作的研二学生,写博客一方面是复习总结大数据开发的知识点,一方面是希望帮助更多自学的小伙伴.由于老刘是自学大数据开发,肯定会存在一些不足,还希望大家能够批评指正,让我们一起进步! ...

  6. IDEA配置JRebel实现热部署

    IDEA配置JRebel实现热部署 1.下载JRebel and XRebel for Intellij插件 2. 激活 请查看这个文章http://www.cicoding.cn/other/jre ...

  7. mmall商城用户模块开发总结

    1.需要实现的功能介绍 注册 登录 用户名校验 忘记密码 提交问题答案 重置密码 获取用户信息 更新用户信息 退出登录 目标: 避免横向越权,纵向越权的安全漏洞 MD5明文加密级增加的salt值 Gu ...

  8. LeetCode559.N 叉树的最大深度

    题目 法一.层序遍历 1 class Solution { 2 public: 3 int maxDepth(Node* root) { 4 if(root== NULL) return 0; 5 q ...

  9. C# ADO.NET连接字符串详解

    C#中连接字符串包含以下内容 参数 说明 Provider 设置或者返回提供的连接程式的名称,仅用于OLeDbConnection对象 Connection Timeout 在终止尝试并产生异常前,等 ...

  10. 关于JDK15的简单理解

    一.为什么要了解JDK15? 2020年9月15日,Oracle官方发布了JDK15版本,及时关注官方的更新动态,可以让我们在日常开发中更合理的选择更加优秀的工具方法,避免使用一些过时的或一些即将被删 ...