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. insert into varchar2(8000)

    在看12c的文档的时候发现varcahr2最大长度是4000 byte VARCHAR2 Data Type The VARCHAR2 data type specifies a variable-l ...

  2. SVN:通过命令行只提交目录,并忽略其中的文件

    通过SVN命令行,我们可以只提交项目的目录结构,从而绕开一开始就将我们不想要上传的文件给提交到服务器上.命令如下:svn add -N [directory] 之后可以进入此目录,执行以下命令:svn ...

  3. Codeforces Round #277 (Div. 2) D. Valid Sets DP

    D. Valid Sets   As you know, an undirected connected graph with n nodes and n - 1 edges is called a  ...

  4. 【Ubuntu QQ】记如何在Ubuntu上安装QQ(附下载)

    什么困扰着一批批的ubuntu桌面用户?是麻花藤.哦不,是QQ,怎么在ubuntu上安装完美无瑕的QQ. 最佳解决方案在“三”部分,当然前两个也不失为解决方案 一.尝试的开始 配置: 双系统:Wind ...

  5. luogu1726 上白泽慧音

    题目大意 求一个有向图含节点数最多且结点编号从小到大排列字典序最小的强连通分量. 注意事项 HDU1269那道题题面.数据太弱,在这道题上把我害惨了... Dfs点u时,如果与u相连的一个点v有Dfs ...

  6. 使用poi读取word2007(.docx)中的复杂表格

    使用poi读取word2007(.docx)中的复杂表格 最近工作需要做一个读取word(.docx)中的表格,并以html形式输出.经过上网查询,使用了poi. 对于2007及之后的word文档,需 ...

  7. 历届试题 邮局(dfs+剪枝)

      历届试题 邮局   时间限制:1.0s   内存限制:256.0MB      问题描述 C村住着n户村民,由于交通闭塞,C村的村民只能通过信件与外界交流.为了方便村民们发信,C村打算在C村建设k ...

  8. TCP打开文件传输(服务器端并发code)

    #include <stdio.h>#include <stdlib.h>#include <arpa/inet.h>#include <sys/types. ...

  9. python多线程,限制线程数

    #encoding:utf8 import threading import time data = 0 def func(sleeptime): global data print threadin ...

  10. LeetCode.5-最长回文子串(Longest Palindromic Substring)

    这是悦乐书的第342次更新,第366篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第3题(顺位题号是5).给定一个字符串s,找到s中最长的回文子字符串. 您可以假设s ...