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. PHP array_flip()

    定义和用法 array_flip() 函数返回一个反转后的数组,如果同一值出现了多次,则最后一个键名将作为它的值,所有其他的键名都将丢失. 如果原数组中的值的数据类型不是字符串或整数,函数将报错. 语 ...

  2. ubuntu下的代码格式化工具

    indent -kr -i4   SimpleMail.java 可以用,但是太简单 astyle 这个工具不错 http://astyle.sourceforge.net/ astyle --sty ...

  3. 大数据处理之道(实验方法&lt;二&gt;)

    一:交叉验证(crossvalidation)(附实验的三种方法)方法简单介绍   (1) 定义:交叉验证(Cross-validation)主要用于建模应用中,比如PCR(Principal Com ...

  4. iOS7系统iLEX RAT冬青鼠安装教程:无需刷机还原纯净越狱系统

    全网科技 温馨提醒:iLEX RAT和Semi-Restore的作用都是让你的已越狱的设备恢复至越狱的初始状态. 可是要注意无论你是用iLexRAT冬青鼠还是Semi-restore.对于还原来说都存 ...

  5. Android入门之文件系统操作(一)简单的文件浏览器

    版权声明:本文为博主原创文章,未经博主允许不得转载.       import java.io.File; import java.util.*; import android.app.Activit ...

  6. 将tflearn的模型保存为pb,给TensorFlow使用

    参考:https://github.com/tflearn/tflearn/issues/964 解决方法: """ Tensorflow graph freezer C ...

  7. .NET通用基本权限系统框架源代码

    DEMO下载地址: http://download.csdn.net/detail/shecixiong/5372895 一.开发技术:B/S(.NET C# ) 1.Windows XP以上 (支援 ...

  8. IJ:ALI OSS 配置

    ylbtech-IJ:ALI OSS 配置 1. src/resources/返回顶部 1.src/resources/ 1.1.aliyunoss.properties # oss\u7684\u5 ...

  9. 97. ExtJS之EditorGridPanel afteredit属性

    转自:https://zccst.iteye.com/blog/1328869 1. 之前大多用Ext.grid.GridPanel,现在需要可编辑功能,发现比以前稍复杂一些. 就是需要对指定列进行可 ...

  10. Tunnel Warfare(树状数组+二分)

    http://poj.org/problem?id=2892 题意:输入n,m.n代表数轴的长度,m代表操作数. D x: 摧毁点x Q x: 询问村庄x最左与最右没有被摧毁的点的距离 R  :恢复最 ...