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 < m
10000, 0
k
3000000)<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 ...
随机推荐
- Android之QQ授权登录获取用户信息
有时候我们开发的app须要方便用户简单登录.能够让用户使用自己的qq.微信.微博登录到我们自己开发的app. 今天就在这里总结一下怎样在自己的app中集成QQ授权登录获取用户信息的功能. 首先我们打开 ...
- URAL 1196. History Exam (二分)
1196. History Exam Time limit: 1.5 second Memory limit: 64 MB Professor of history decided to simpli ...
- DotNetBar.Bar作为容器使用的方法及Text更新原理
DotNetBar.Bar作为容器使用的方法及Text更新原理 老帅 一.容器用法 控件DevComponents.DotNetBar.Ba ...
- 查找存在某字符的文件列表,不包括svn文件
find . ! -wholename '*.svn*' -print | xargs grep "img" | awk -F ':.' '{print $1}' | uniq
- 试试pypy
pypy是一个python的解释器和JIT编译器.能够在不改动不论什么代码的情况下大幅提升python代码的性能. 使用超级简单,在官网下载编译好的二进制包进行安装,然后然后执行代码的时候指定这个解释 ...
- [C++设计模式] composite 组合模式
组合(Composite)模式的其他翻译名称也非常多,比方合成模式.树模式等等.在<设计模式>一书中给出的定义是:将对象以树形结构组织起来,以达成"部分-总体"的层次结 ...
- Android EditText技巧总结
一.默认不获取焦点: 在布局文件的父控件中,设置如下属性: android:focusable="true" android:focusableInTouchMode=" ...
- Codesys——常用快捷键列表
F1——打开Help文档: F2——打开Input Assistant: F5——执行程序(Start): F9——添加或取消断点(Toggle Breakpoint): F8——单步进入(Step ...
- H264的RTP负载打包的数据包格式,分组,分片
H264的RTP负载打包的数据包格式,分组,分片 1. RTP数据包格式 RTP报文头格式(见RFC3550 Page12): 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 ...
- 协议-网络-安全协议:SSH(安全外壳协议)
ylbtech-协议-网络-安全协议:SSH(安全外壳协议) SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定:SSH 为建立 ...