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

Problem Description
Panagola,
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.

 
Input
There are multiple test cases. Input ends with a line containing three zeros.
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.

 
Output
For each test case, output the sum of welcome values of the best route. The answer can be fit in a 32 bits integer.
 
Sample Input
2 3 2
7 8 1
4 5 6
1 2 3
1 1 1
1 1 1
1 1 1
0 0 0
 
Sample Output
27
 
Source
   类似于传统的走格子,不过加了一个横向限制条件,横着连续走的格子属性之和不可大于一个固定值,另外可以左右双向移动。
题目说是从下方走到上方,自然可以倒过来思考,用我们熟悉的从上至下递推,一开始用错了递推式: f[i][j][k]=MAX{f[i-1][j][0],f[i-1][j][1],f[i][j-1][k]|f[i][j+1][k] },
我想用第三维表示行走的不同方向分别对应的最值,但是后来发现了是错误的!由于有这个限制,可能此格子最优解不能由相邻格子的最优解推导出来导致计算错误。
  但是显然每一行对应的格子一定是由上一行的格子推导而来的,有两种形式,一是由上面的格子下来后往左走到达,另一种就是往右走到达,显然我们可以分类讨论。
说一下向右到达的,f[i][j]=MAX{ f[i-1][k]+w[j]-w[k] | 1<=k<=j&&p[j]-p[k]<=K }
这个递推式中w[j]是已知的,对于f[i-1][k]-w[k],我们对他用优先队列维护一个最大值,对每一行从左至右递推,每次push一个f[i-1][j]+w[j]来保证 k<=j这个条件成立。
对于队首不满足当前格子的节点,显然对后面的格子更不会满足,直接pop掉即可。这样的渐进复杂度在O(n*m)间,可以完成。
类似的对另一个方向,倒序递推一遍就好了!
 #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的更多相关文章

  1. 【单调队列优化dp】HDU 3401 Trade

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 [题意] 知道之后n天的股票买卖价格(api,bpi),以及每天股票买卖数量上限(asi,bsi),问他最 ...

  2. 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 ...

  3. Parade(单调队列优化dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others)    ...

  4. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  5. 单调队列优化DP——习题收集

    前言 感觉可以用单调队列优化dp的模型还是挺活的,开个随笔记录一些遇到的比较有代表性的模型,断续更新.主要做一个收集整理总结工作. 记录 0x01 POJ - 1821 Fence,比较适合入门的题, ...

  6. 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 ...

  7. hdu3401:单调队列优化dp

    第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...

  8. BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP

    BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP Description 有一排n棵树,第i棵树的高度是Di. MHY要从第一棵树到第n棵树去找他的妹子玩. 如果MHY在 ...

  9. 【单调队列优化dp】 分组

    [单调队列优化dp] 分组 >>>>题目 [题目] 给定一行n个非负整数,现在你可以选择其中若干个数,但不能有连续k个数被选择.你的任务是使得选出的数字的和最大 [输入格式] ...

随机推荐

  1. java基础—Hashtable,HashMap,TreeMap的差别

    Hashtable : 此类实现一个哈希表,该哈希表将键映射到对应的值.不论什么非null  对象都能够作键值,是线程不同步的 HashMap : 基于哈希表的Map接口的实现.此实现提供全部可选的映 ...

  2. 转载:阮一峰 理解RESTful架构

    转载 http://www.ruanyifeng.com/blog/2011/09/restful.html 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件&q ...

  3. 字符串之_strncat

    功能:将src的前n个字符添加到dest的后面 输入:dest,src,count 返回:ret #include <iostream> #include <assert.h> ...

  4. uwsgi+nginx项目上线

    一.基础环境配置 1.Linux安装配置     1.设置IP地址 [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 ...

  5. 剑指offer 面试63题

    面试63题 题目:股票的最大利润 题:假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可获得的最大利润是多少?例如,一只股票在某些时间节点的价格为{9,11,8,5,7,12,16, ...

  6. JavaScript Date to c# Ticks

    var ticks =((yourDateObject.getTime()*10000)+621355968000000000); var minDate = new Date("2013& ...

  7. Linux:Ubuntu16.04下创建Wifi热点

    Linux:Ubuntu16.04下创建Wifi热点说明 1.Ubuntu16.04里面可以直接创建热点,而不用像以前的版本,还要其他辅助工具. 2.本篇文章参考自编程人生 具体步骤如下: 1. 点击 ...

  8. $《第一行代码:Android》读书笔记——第13章 Android高级技巧

    (一)全局获取Context 1.创建ApplicationUtil类继承自Application类: public class ApplicationUtil extends Application ...

  9. Java数据类型 及 转换原则

    一.数据类型分类:主要分为 基本类型.引用类型两大类: 二.基本类型 转换原则 1.类型转换主要在在 赋值.方法调用.算术运算 三种情况下发生. a.赋值和方法调用 转换规则:从低位类型到高位类型自动 ...

  10. css float 浮动是个混球

    得说,在学习 CSS 的时候就一直在疑惑,横排布局干嘛要用 float 这种属性, 用了还会高度塌陷,怎么不发明些高级点的属性来完成横排布局呢, 甚至所有人都告诉我摒弃表格布局,浮动布局才是 DIV+ ...