LA 4327
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 <tex2html_verbatim_mark>west-east roads and m + 1 <tex2html_verbatim_mark>north-south roads. Of course, there are (n + 1)×(m + 1) <tex2html_verbatim_mark>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 <tex2html_verbatim_mark>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 <tex2html_verbatim_mark>minutes. If it takes p <tex2html_verbatim_mark>minutes to pass a love-hate zone, we say the length of that love-hate zone is p <tex2html_verbatim_mark>. 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 <tex2html_verbatim_mark>lines.
The first line contains three integers: n <tex2html_verbatim_mark>, m <tex2html_verbatim_mark>and k <tex2html_verbatim_mark>. (0 < n100, 0 < m10000, 0k3000000)<tex2html_verbatim_mark>
The next n + 1 <tex2html_verbatim_mark>lines stands for n + 1 <tex2html_verbatim_mark>west-east roads in north to south order. Each line contains m <tex2html_verbatim_mark>integers showing the welcome values of the road's m <tex2html_verbatim_mark>love-hate zones, in west to east order.
The last n + 1 <tex2html_verbatim_mark>lines also stands for n + 1 <tex2html_verbatim_mark>west-east roads in north to south order. Each line contains m<tex2html_verbatim_mark>integers showing the lengths (in minutes) of the road's m <tex2html_verbatim_mark>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 dp模型不难想,单调队列从左到右,从右到左优化一次就可以了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; #define read() freopen("sw.in", "r", stdin) typedef long long ll;
const int MAX_N = ;
const int MAX_M = ;
const ll INF = 1e10 + ;
int N, M, K;
int wv[MAX_N][MAX_M];
int ma[ * MAX_M];
ll dp[MAX_N][MAX_M];
ll sumt[MAX_N][MAX_M];
ll sub[MAX_M]; void solve() {
memset(dp, , sizeof(dp)); for (int i = N + ; i >= ; --i) {
int s = , e = ;
ll now = ;
memset(sub, , sizeof(sub));
for (int j = ; j <= M + ; ++j) { if (s < e)
dp[i][j] = max(dp[i][j], dp[i + ][ ma[s] ] + now + sub[ ma[s] ]);
dp[i][j] = max(dp[i + ][j], dp[i][j]);
while (s < e && dp[i + ][j] > dp[i + ][ ma[e - ] ] + now + sub[ ma[e - ] ]) --e;
ma[e++] = j;
sub[j] = -now;
now += wv[i][j]; while (s < e && sumt[i][j + ] - sumt[i][ ma[s] ] > K) {
++s; }
} s = , e = , now = ;
memset(sub, , sizeof(sub));
for (int j = M + ; j >= ; --j) {
if (s < e)
dp[i][j] = max(dp[i][j], dp[i + ][ ma[s] ] + now + sub[ ma[s] ]);
dp[i][j] = max(dp[i + ][j], dp[i][j]);
while (s < e && dp[i + ][j] > dp[i + ][ ma[e - ] ] + now + sub[ ma[e - ] ]) --e;
ma[e++] = j;
sub[j] = -now;
now += wv[i][j - ]; while (s < e && sumt[i][ ma[s] ] - sumt[i][j - ] > K) {
++s;
}
}
} ll ans = -INF;
for (int i = ; i <= M + ; ++i) {
ans = max(ans, dp[][i]);
} printf("%lld\n", ans); } int main()
{
//read();
while (~scanf("%d%d%d", &N, &M, &K) && (N || M || K)) {
for (int i = ; i <= N + ; ++i) {
for (int j = ; j <= M; ++j) {
scanf("%d", &wv[i][j]);
}
} memset(sumt, , sizeof(sumt)); for (int i = ; i <= N + ; ++i) {
for (int j = ; j <= M; ++j) {
int ch;
scanf("%d", &ch);
sumt[i][j + ] += sumt[i][j] + ch;
}
} solve();
}
return ;
}
LA 4327的更多相关文章
- LA 4327 多段图
题目链接:https://vjudge.net/contest/164840#problem/B 题意: 从南往北走,横向的时间不能超过 c: 横向路上有权值,求权值最大: 分析: n<=100 ...
- LA 4327 Parade(单调队列优化dp)
题目链接: 题目大意(摘自刘汝佳<<算法竞赛入门经典--训练指南>>):F城是由n+1条横向路和m+1条竖向路组成.你的任务是从最南边的路走到最北边的路,使得走过的路上的高兴值 ...
- leggere la nostra recensione del primo e del secondo
La terra di mezzo in trail running sembra essere distorto leggermente massima di recente, e gli aggi ...
- Le lié à la légèreté semblait être et donc plus simple
Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...
- Mac Pro 使用 ll、la、l等ls的别名命令
在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...
- Linux中的动态库和静态库(.a/.la/.so/.o)
Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序编译的过程 .o文件(目标文件) 创建atoi.o 使用atoi. ...
- Mac OS使用ll、la、l等ls的别名命令
在linux下习惯使用ll.la.l等ls别名的童鞋到mac os可就郁闷了-- 其实只要在用户目录下建立一个脚本“.bash_profile”,并输入以下内容即可: alias ll='ls -al ...
- .Uva&LA部分题目代码
1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...
- 获取在线人数 CNZZ 和 51.la
string Cookies = string.Empty; /// <summary> /// 获取在线人数 (51.la统计器) /// </summary> /// &l ...
随机推荐
- [ACM] POJ 3295 Tautology (构造)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9302 Accepted: 3549 Descrip ...
- Android push推送消息到达成功率优化
Android push推送消息到达成功率优化 问题:server向client发送消息.未考虑client是否在线,这种消息到达率是非常低的. 第一次优化:使用server离线缓存数据,推断假设cl ...
- C++操作Json字符串
一.从字符串中读取JSON a.cpp ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...
- HDU 3572 Task Schedule(ISAP模板&&最大流问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3572 题意:m台机器.须要做n个任务. 第i个任务.你须要使用机器Pi天,且这个任务要在[Si , ...
- 基于ArcGIS Flex API实现动态标绘(1.1)
动态标绘API 1.1版本号.相较前一版本号1.0(点击进入).该版本号提供标绘符号的编辑功能. 编辑功能包含两种编辑状态:编辑控制点.对标绘符号进行旋转.八方向拉伸.平移. 编辑控制点例如以下图所看 ...
- js简单函数封装
//每index个字符插入一个str字符串 String.prototype.insertStrPerIndex =function(index,str){ if(this.length>ind ...
- SecureCRT——设置打印中文字符
1. 设置方法 使用SecureCRT打印由STM32发送的中文字符提示信息,显示乱码.在网上找了一些链接,再加上自己摸索,终于出了能够让SecureCRT打印中文的方法. 设置以下几个地方即可. 1 ...
- javascript--给你的JS代码添加单元测试
通过测试框架为JavaScript应用添加测试,从而保证代码的高质量.这里的笔记例子应用在jaywcjlove/validator.js中. 安装 用到三个工具chai(断言工具),mocha(测试框 ...
- Candies(差分约束系统)
http://poj.org/problem?id=3159 思路:用O(V+ElogV)的Dijkstra算法求1到n的最短路.即用优先队列优化Dijkstra算法. #include <st ...
- jquery的ajax同步异步执行
大家先看一段简单的jquery ajax 返回值的js 代码 function getReturnAjax{ $.ajax({ type:"POST", http:/ ...