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.

<tex2html_verbatim_mark>

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的更多相关文章

  1. LA 4327 多段图

    题目链接:https://vjudge.net/contest/164840#problem/B 题意: 从南往北走,横向的时间不能超过 c: 横向路上有权值,求权值最大: 分析: n<=100 ...

  2. LA 4327 Parade(单调队列优化dp)

    题目链接: 题目大意(摘自刘汝佳<<算法竞赛入门经典--训练指南>>):F城是由n+1条横向路和m+1条竖向路组成.你的任务是从最南边的路走到最北边的路,使得走过的路上的高兴值 ...

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

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

  5. Mac Pro 使用 ll、la、l等ls的别名命令

    在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...

  6. Linux中的动态库和静态库(.a/.la/.so/.o)

    Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序编译的过程 .o文件(目标文件) 创建atoi.o 使用atoi. ...

  7. Mac OS使用ll、la、l等ls的别名命令

    在linux下习惯使用ll.la.l等ls别名的童鞋到mac os可就郁闷了-- 其实只要在用户目录下建立一个脚本“.bash_profile”,并输入以下内容即可: alias ll='ls -al ...

  8. .Uva&LA部分题目代码

    1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...

  9. 获取在线人数 CNZZ 和 51.la

    string Cookies = string.Empty; /// <summary> /// 获取在线人数 (51.la统计器) /// </summary> /// &l ...

随机推荐

  1. MySQL的各种网络IO超时的用法和实现

    2016-04-06 赵伟 数据库开发者 客户端C API 在C API中调用mysql_options()来设置mysql_init() 所创建的连接对象的属性,使用这三个选项可以设置连接超时和读写 ...

  2. 淘宝信海龙 --PHP系统

    https://yq.aliyun.com/users/1467229535950742?spm=5176.100239.blogrightarea56002.3.RoToxZ

  3. [转]十五天精通WCF——第一天 三种Binding让你KO80%的业务

    转眼wcf技术已经出现很多年了,也在.net界混的风生水起,同时.net也是一个高度封装的框架,作为在wcf食物链最顶端的我们所能做的任务已经简单的不能再简单了, 再简单的话马路上的大妈也能写wcf了 ...

  4. 笔记本光驱位换SSD固态硬盘之硬盘格式化

    笔记本光驱位换SSD固态硬盘之硬盘格式化 系列文章: ThinkPad E430c加装内存和SSD固态硬盘 笔记本光驱位换SSD固态硬盘之Ghost克隆原来的系统到SSD固态硬盘分区 概述 加装SSD ...

  5. Android shape自定义形状,设置渐变色

      <?xml version="1.0" encoding="utf-8"?> <!-- android:shape=["rect ...

  6. alsa 用户空间编程【转】

    本文转载自:http://blog.csdn.net/sjin_1314/article/details/12872581 /**alsa play test *ALSA用户空间编译,ALSA驱动的声 ...

  7. hdu 1213(并查集模版题)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. javascript 将变量值作为对象属性 获取对象对应的值

    例子 var var="name"; var objname="obj"; objname=objname+"."+var; alert(e ...

  9. 学习XOR

    //f(x;W,c,w,b)=w*max{0, W*x+c}+b #include <iostream>#include <vector>#include <algori ...

  10. CSS--浏览器CSS Hack 收集

    所谓的Hack就是只有特定浏览器才能识别这段hack代码.Hack 不是什么好东西,除非没有办法,我们尽量还是不要用着玩意. 下面是各个浏览器的CSS Hack 列表. Firefox 浏览器 @-m ...