hdu 2490 队列优化dp
http://acm.hdu.edu.cn/showproblem.php?pid=2490
Parade
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1145 Accepted Submission(s): 527
The Lord of city F likes to parade very much. He always inspects his
city in his car and enjoys the welcome of his citizens. City F has a
regular road system. It looks like a matrix with n+1 west-east roads and
m+1 north-south roads. Of course, there are (n+1)×(m+1) road crosses in
that system. The parade can start at any cross in the southernmost road
and end at any cross in the northernmost road. Panagola will never
travel from north to south or pass a cross more than once. Citizens will
see Panagola along the sides of every west-east road. People who love
Panagola will give him a warm welcome and those who hate him will throw
eggs and tomatoes instead. We call a road segment connecting two
adjacent crosses in a west-east road a “love-hate zone”. Obviously
there are m love-hate zones in every west-east road. When passing a
love-hate zone, Panagola may get happier or less happy, depending on how
many people love him or hate him in that zone. So we can give every
love-hate zone a “welcome value” which may be negative, zero or
positive. As his secretary, you must make Panagola as happy as possible.
So you have to find out the best route ----- of which the sum of the
welcome values is maximal. You decide where to start the parade and
where to end it.
When seeing his Citizens, Panagola
always waves his hands. He may get tired and need a break. So please
never make Panagola travel in a same west-east road for more than k
minutes. If it takes p minutes to pass a love-hate zone, we say the
length of that love-hate zone is p. Of course you know every love-hate
zone’s length.
The figure below illustrates the case in sample input. In this figure, a best route is marked by thicker lines.
Each test case consists of 2×n + 3 lines.
The first line contains three integers: n, m and k.(0<n<=100,0<m<=10000, 0<=k<=3000000)
The
next n+1 lines stands for n + 1 west-east roads in north to south
order. Each line contains m integers showing the welcome values of the
road’s m love-hate zones, in west to east order.
The last n+1
lines also stands for n + 1 west-east roads in north to south order.
Each line contains m integers showing the lengths (in minutes) of the
road's m love-hate zones, in west to east order.
7 8 1
4 5 6
1 2 3
1 1 1
1 1 1
1 1 1
0 0 0
#include <iostream>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<queue>
#include<cstring>
#include<ctype.h>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long LL;
const int MAX = ;
struct node {
int w, id ;
bool operator<(const node &tmp)const {
return w < tmp.w;
}
};
int f[][];
int w[][], p[][];
int main()
{
int n, m, i, j, k;
while (scanf("%d%d%d", &n, &m, &k) == && (n + m + k)) {
memset(f, , sizeof(f));
for (i = ;i <= n + ;++i)
{
w[i][] = ;
for (j = ;j <= m + ;++j)
{
scanf("%d", &w[i][j]);
w[i][j] += w[i][j - ];
}
}
for (i = ;i <= n + ;++i)
{
p[i][] = ;
for (j = ;j <= m + ;++j)
{
scanf("%d", &p[i][j]);
p[i][j] += p[i][j - ];
}
}
for (i = ;i <= n + ;++i)
{
priority_queue<node>Q;
for (j = ;j <= m + ;++j)
{
Q.push(node{f[i-][j]-w[i][j],j});
while (!Q.empty() && p[i][j]-p[i][Q.top().id]>k)Q.pop();
if (!Q.empty()) f[i][j] = Q.top().w + w[i][j];
}
priority_queue<node>P;
for (j = m + ;j >= ;--j)
{
P.push(node{ f[i - ][j] + w[i][j],j });
while (!P.empty() && p[i][P.top().id] - p[i][j] > k)P.pop();
if (!P.empty()) f[i][j] = max(f[i][j],P.top().w-w[i][j]);
}
}
int ans = ;
for (i = ;i <= m + ;++i)
ans = max(ans, f[n + ][i]);
printf("%d\n",ans );
}
return ;
}
hdu 2490 队列优化dp的更多相关文章
- 【单调队列优化dp】HDU 3401 Trade
http://acm.hdu.edu.cn/showproblem.php?pid=3401 [题意] 知道之后n天的股票买卖价格(api,bpi),以及每天股票买卖数量上限(asi,bsi),问他最 ...
- bzoj1855: [Scoi2010]股票交易 单调队列优化dp ||HDU 3401
这道题就是典型的单调队列优化dp了 很明显状态转移的方式有三种 1.前一天不买不卖: dp[i][j]=max(dp[i-1][j],dp[i][j]) 2.前i-W-1天买进一些股: dp[i][j ...
- Parade(单调队列优化dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others) ...
- 单调队列优化DP,多重背包
单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...
- 单调队列优化DP——习题收集
前言 感觉可以用单调队列优化dp的模型还是挺活的,开个随笔记录一些遇到的比较有代表性的模型,断续更新.主要做一个收集整理总结工作. 记录 0x01 POJ - 1821 Fence,比较适合入门的题, ...
- bzoj1855: [Scoi2010]股票交易--单调队列优化DP
单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w ...
- hdu3401:单调队列优化dp
第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...
- BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP
BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP Description 有一排n棵树,第i棵树的高度是Di. MHY要从第一棵树到第n棵树去找他的妹子玩. 如果MHY在 ...
- 【单调队列优化dp】 分组
[单调队列优化dp] 分组 >>>>题目 [题目] 给定一行n个非负整数,现在你可以选择其中若干个数,但不能有连续k个数被选择.你的任务是使得选出的数字的和最大 [输入格式] ...
随机推荐
- python3 中引用 HTMLTestRunner.py 模块的注意事项
HTMLTestRunner.py支持python2中运行,如果在python3.6.2中引用HTMLTestRunner.py模块,需要做一下更改: 1.更改HTMLTestRunner.py模块中 ...
- 《Python 数据分析》笔记——pandas
Pandas pandas是一个流行的开源Python项目,其名称取panel data(面板数据)与Python data analysis(Python 数据分析)之意. pandas有两个重要的 ...
- Latex技巧:LaTex插图命令includegraphics参数详解
Latex插图的命令是\includegraphics[选项]{文件} 这里的选项在表 7.1, 7.2, 7.3 中列出. 因为 \includegraphics 不会结束 当前段落,所以它能够在文 ...
- C#(ASP.NET)隐藏或显示Excel中指定列
今天写的一个方法,实现Excel指定列的隐藏和显示: 环境:VS2010,OFFICE 2010 代码:#region 隐藏和显示Excel中的一列 /// <summary> ...
- hibernate 操作 Postgresql 数据库报 operator does not exist: integer = character varying
网上的说法如下: Java开发Postgresql 数据库兼容应用的问题,与Oracle有一些不同: Java类型映射数据库类型的不同,Oracle jdbc驱动程序处理Java String类型可正 ...
- Linux文件系统管理 parted分区命令
概述 parted 命令是可以在命令行直接分区和格式化的,不过 parted 交互模式才是更加常用的命令方式. parted命令 进入交互模式命令如下: [root@localhost ~]# par ...
- 【HackerRank】Encryption
One classic method for composing secret messages is called a square code. The spaces are removed fr ...
- Android Opencv NativeCameraView error in 5.0 lollipop versions (Bug #4185)
https://github.com/opencv/opencv/wiki http://code.opencv.org/issues/4185 Hello, I finally get a ride ...
- 写时拷贝(Copy On Write)方案详解
本文旨在通过对 写时拷贝 的四个方案(Copy On Write)分析,让大家明白写时拷贝的实现及原理. 关于浅拷贝与深拷贝,我在之前的博客中已经阐述过了 浅拷贝容易出现指针悬挂的问题,深拷贝效率低 ...
- freemarker模板解析过程
例如:一个freemarker表达式<body> ${hello} </body>,会被解析成三个部分,分别是<body>${hello}</body> ...