bzoj 1855 dp + 单调队列优化
思路:很容易写出dp方程,很容易看出能用单调队列优化。。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = ; int n, m, w, ap[N], bp[N], as[N], bs[N], dp[N][N];
int stk[N], head, rear; int cal1(int p, int i, int x) {
return dp[p][x] + x * ap[i];
} int cal2(int p, int i, int x) {
return dp[p][x] + x * bp[i];
} int main() {
scanf("%d%d%d", &n, &m, &w);
for(int i = ; i <= n; i++)
scanf("%d%d%d%d", &ap[i], &bp[i], &as[i], &bs[i]); for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
dp[i][j] = -inf;
dp[][] = ; int ans = ;
for(int i = ; i <= n; i++) {
head = , rear = ;
int p = max(, i - w - ); for(int j = ; j <= m; j++) dp[i][j] = dp[i - ][j];
for(int j = ; j <= m; j++) {
while(rear >= head && cal1(p, i, j) > cal1(p, i, stk[rear])) rear--;
stk[++rear] = j;
while(j - as[i] > stk[head]) head++;
dp[i][j] = max(dp[i][j], cal1(p, i, stk[head]) - j * ap[i]);
} head = , rear = ;
for(int j = m; j >= ; j--) {
while(rear >= head && cal2(p, i, j) > cal2(p, i, stk[rear])) rear--;
stk[++rear] = j;
while(j + bs[i] < stk[head]) head++;
dp[i][j] = max(dp[i][j], cal2(p, i, stk[head]) - j * bp[i]);
ans = max(ans, dp[i][j]);
}
} printf("%d\n", ans);
return ;
} /*
*/
bzoj 1855 dp + 单调队列优化的更多相关文章
- BZOJ 1855 股票交易 - 单调队列优化dp
传送门 题目分析: \(f[i][j]\)表示第i天,手中拥有j份股票的最优利润. 如果不买也不卖,那么\[f[i][j] = f[i-1][j]\] 如果买入,那么\[f[i][j] = max\{ ...
- [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)
DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...
- 1023: [SHOI2008]cactus仙人掌图(DP+单调队列优化)
这道题吗= =首先解决了我多年以来对仙人掌图的疑问,原来这种高大上的东西原来是这个啊= = 然后,看到这种题,首先必须的就是缩点= = 缩点完之后呢,变成在树上找最长路了= =直接树形dp了 那么那些 ...
- Codeforces 1077F2 Pictures with Kittens (hard version)(DP+单调队列优化)
题目链接:Pictures with Kittens (hard version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:数据量5000, ...
- P3084 [USACO13OPEN]照片Photo (dp+单调队列优化)
题目链接:传送门 题目: 题目描述 Farmer John has decided to assemble a panoramic photo of a lineup of his N cows ( ...
- Codeforces 445A Boredom(DP+单调队列优化)
题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...
- 股票交易(DP+单调队列优化)
题目描述 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股票的走势,第i天的股票买入价为每股APi, ...
- Luogu 2627 修建草坪 (动态规划Dp + 单调队列优化)
题意: 已知一个序列 { a [ i ] } ,求取出从中若干不大于 KK 的区间,求这些区间和的最大值. 细节: 没有细节???感觉没有??? 分析: 听说有两种方法!!! 好吧实际上是等价的只是看 ...
- 【简洁易懂】CF372C Watching Fireworks is Fun dp + 单调队列优化 dp优化 ACM codeforces
题目大意 一条街道有$n$个区域. 从左到右编号为$1$到$n$. 相邻区域之间的距离为$1$. 在节日期间,有$m$次烟花要燃放. 第$i$次烟花燃放区域为$a_i$ ,幸福属性为$b_i$,时间为 ...
随机推荐
- eclipse中配置jbpm3.2插件
1.什么是jbpm?为什么要使用jbpm呢? 通俗一点讲,jbpm是一个负责管理工作流的一个产品,那么什么是工作流呢,所谓的工作流就是在办公自动化系统中,提交申请,申请经过多个部门领导审批,完成该流程 ...
- [Java多线程]-J.U.C.atomic包下的AtomicInteger,AtomicLong等类的源码解析
Atomic原子类:为基本类型的封装类Boolean,Integer,Long,对象引用等提供原子操作. 一.Atomic包下的所有类如下表: 类摘要 AtomicBoolean 可以用原子方式更新的 ...
- 为VSCODE添加右键菜单
参考:https://blog.csdn.net/GreekMrzzJ/article/details/82194913 1.创建一个名为vscode.reg的空文本文件,填入下列内容 Windows ...
- CountUp.js让页面数字跳动起来
CountUp.js 无依赖的.轻量级的 JavaScript 类,可以用来快速创建以一种更有趣的动画方式显示数值数据.尽管它的名字叫 countUp,但其实可以在两个方向进行变化,这是根据你传递的 ...
- 51nod 1171 大灾变
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1757 二分答案mid 避难所拆为mid个点 每个避难所的第一个点向第二个 ...
- Codeforces 620C EDU C.Pearls in a Row ( set + greed )
C. Pearls in a Row There are n pearls in a row. Let's enumerate them with integers from 1 to n from ...
- Python学习笔记(三十五)—内置模块(4)struct
摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431955007 ...
- Handlerbars基础笔记
此笔记摘抄于杨元的博客(http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) 引入 <script type=&qu ...
- 【BZOJ】2004: [Hnoi2010]Bus 公交线路 状压DP+矩阵快速幂
[题意]n个点等距排列在长度为n-1的直线上,初始点1~k都有一辆公车,每辆公车都需要一些停靠点,每个点至多只能被一辆公车停靠,且每辆公车相邻两个停靠点的距离至多为p,所有公车最后会停在n-k+1~n ...
- bzoj 2741 可持久化trie
首先我们设si为前i个数的xor和,那么对于询问区间[i,j]的xor和,就相当于si-1^sj,那么对于这道题的询问我们可以处理处si,然后对于询问[l,r],可以表示为在区间[l-1,r]里找两个 ...