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 ;
}
最新文章
- Vue插件开发入门
- Git 取消跟踪已版本控制的文件
- 可靠UDP
- c#绘制表格
- python积累
- sql查找最小缺失值与重用被删除的键(转载)
- poj3101
- Swift Core Data 图片存储与读取Demo
- shell 中条件判断
- 输出A打头的字符串
- 51nod 区间中第K大的数
- thinkphp调试技巧
- TortoiseGit之配置密钥
- JS功能函数
- FileZilla 使用笔记
- 应用脚手架创建一个React项目
- Linux入门笔记
- jquery插件中找到好玩插件 http://www.jq22.com/
- 《Linux 性能及调优指南》2.3 监控工具
- 吴裕雄 04-mysql创建数据库