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.

<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模型不难想,单调队列从左到右,从右到左优化一次就可以了

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7
 8 using namespace std;
 9
10 #define read() freopen("sw.in", "r", stdin)
11
12 typedef long long ll;
13 const int MAX_N = 105;
14 const int MAX_M = 10006;
15 const ll INF = 1e10 + 7;
16 int N, M, K;
17 int wv[MAX_N][MAX_M];
18 int ma[2 * MAX_M];
19 ll dp[MAX_N][MAX_M];
20 ll sumt[MAX_N][MAX_M];
21 ll sub[MAX_M];
22
23 void solve() {
24         memset(dp, 0, sizeof(dp));
25
26         for (int i = N + 1; i >= 1; --i) {
27                 int s = 0, e = 0;
28                 ll now = 0;
29                 memset(sub, 0, sizeof(sub));
30                 for (int j = 1; j <= M + 1; ++j) {
31
32                         if (s < e)
33                         dp[i][j] = max(dp[i][j], dp[i + 1][ ma[s] ] + now + sub[ ma[s] ]);
34                         dp[i][j] = max(dp[i + 1][j], dp[i][j]);
35                         while (s < e && dp[i + 1][j] > dp[i + 1][ ma[e - 1] ] + now + sub[ ma[e - 1] ]) --e;
36                         ma[e++] = j;
37                         sub[j] = -now;
38                         now += wv[i][j];
39
40                         while (s < e && sumt[i][j + 1] - sumt[i][ ma[s] ] > K)  {
41                                 ++s;
42
43                         }
44                 }
45
46                 s = 0, e = 0, now = 0;
47                 memset(sub, 0, sizeof(sub));
48                 for (int j = M + 1; j >= 1; --j) {
49                         if (s < e)
50                         dp[i][j] = max(dp[i][j], dp[i + 1][ ma[s] ] + now + sub[ ma[s] ]);
51                         dp[i][j] = max(dp[i + 1][j], dp[i][j]);
52                         while (s < e && dp[i + 1][j] > dp[i + 1][ ma[e - 1] ] + now + sub[ ma[e - 1] ]) --e;
53                         ma[e++] = j;
54                         sub[j] = -now;
55                         now += wv[i][j - 1];
56
57                         while (s < e && sumt[i][ ma[s] ] - sumt[i][j - 1] > K)  {
58                                 ++s;
59                         }
60                 }
61         }
62
63         ll ans = -INF;
64         for (int i = 1; i <= M + 1; ++i) {
65                 ans = max(ans, dp[1][i]);
66         }
67
68         printf("%lld\n", ans);
69
70 }
71
72 int main()
73 {
74     //read();
75     while (~scanf("%d%d%d", &N, &M, &K) && (N || M || K)) {
76             for (int i = 1; i <= N + 1; ++i) {
77                     for (int j = 1; j <= M; ++j) {
78                             scanf("%d", &wv[i][j]);
79                     }
80             }
81
82             memset(sumt, 0, sizeof(sumt));
83
84             for (int i = 1; i <= N + 1; ++i) {
85                     for (int j = 1; j <= M; ++j) {
86                             int ch;
87                             scanf("%d", &ch);
88                             sumt[i][j + 1] += sumt[i][j] + ch;
89                     }
90             }
91
92             solve();
93     }
94     return 0;
95 }

LA 4327

时间: 2024-10-11 10:21:16

LA 4327的相关文章

动态规划(四)

\(1.String\) \(Painter,Chengdu\) \(2008,LA\) \(4394\) 题意: 给定两个长度相等,只有小写字母组成的字符串\(s\)和\(t\),每步可以把\(s\)的一个连续子串"刷"称同一个字母,问至少需要多少不才能把\(s\)变成\(t\).比如,\(s=bbbbbbb\),\(t=aaabccb\),最少需要两步可实现将\(s\)变成\(t\):\(bbbbbbb->aaabbbb->aaabccb\). 分析: 一个套路的区间\

hdu 5745 la vie en rose

这道题的官方题解是dp,但是可以暴力出来.改天再研究怎么dp. 暴力的时候,如果计算sum的时候,调用strlen函数会超时,可见这个函数并不是十分的好.以后能不用尽量不用. La Vie en rose Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 861    Accepted Submission(s): 461 Problem

让MAC OS也能使用LL LA L等LS的别名

linux下默认ll是ls -l的别名.OS X下默认不支持.习惯了linux下使用ll,我们同样也可以将习惯搬到os x下的shell中. 再当前用户家目录下新建.bash_profile文件.根据你的习惯,添加下面格式内容即可. 1 2 3 alias ll='ls -l' alias la='ls -a' alias l='ls -la' 然后执行:source .bash_profile你还可以添加你喜欢的其他别名.

LA 3942 Remember the Word (Trie)

Remember the Word 题目:链接 题意:给出一个有S个不同单词组成的字典和一个长字符串.把这个字符串分解成若干个单词的连接(单词可以重复使用),有多少种方法? 思路:令d[i]表示从字符i开始的字符串(后缀s[i..L])的分解数,这d[i] = sum{d(i+len(x)) | 单词x是其前缀}.然后将所有单词建成一个Trie树,就可以将搜索单词的复杂度降低. 代码: #include<map> #include<set> #include<queue>

LA 2678 Subsequence

有一个正整数序列,求最短的子序列使得其和大于等于S,并输出最短的长度. 用数组b[i]存放序列的前i项和,所以b[i]是递增的. 遍历终点j,然后在区间[0, j)里二分查找满足b[j]-b[i]≥S的最大的i,时间复杂度为O(nlongn). 这里二分查找用到库函数lower_bound() 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #inclu

LA 4127 - The Sky is the Limit (离散化 扫描线 几何模板)

题目链接 非原创 原创地址:http://blog.csdn.net/jingqi814/article/details/26117241 题意:输入n座山的信息(山的横坐标,高度,山底宽度),计算他们的轮廓线, 即露出来的表面边长,有些山是重叠的不计.空白地带不计,每座山都是等腰三角形. 分析:大白书P414页. 求小山的总长度,用一些虚线将其离散化,分成一段一段的,特征点:山脚,山顶,交点.这样就能保 证相邻两个扫描点之间再无交点.然后一最上面的点就是分割点,维护上一个点lastp即可. 1

LA 2031

Mr. White, a fat man, now is crazy about a game named ``Dance, Dance, Revolution". But his dance skill is so poor that he could not dance a dance, even if he dances arduously every time. Does ``DDR" just mean him a perfect method to squander his

Mac下ll、l、la、等简写命令不能使用

Mac默认用的也是Unix系统,Unix系统本身是没有这些简写命令的,可以通过给命令设置别名来使得可以使用这些简写命令 查看本机所有已经设置的命令别名:alias 设置命令别名:alias ll='ls -alF' 执行命令只在当前shell有效,要长期有效可以设置在用户的.bash_profile里面,这样每次登陆就都可以用这些简写命令了. 步骤: 1.vim ~/.bash_profile 2.将以下内容写入文件并保存 alias ll='ls -alF' alias la='ls -A'

Linux中.a,.la,.o,.so文件的意义和编程实现

Linux中.a,.la,.o,.so文件的意义和编程实现    Linux下文件的类型是不依赖于其后缀名的,但一般来讲:        .o,是目标文件,相当于windows中的.obj文件        .so 为共享库,是shared object,用于动态连接的,和dll差不多        .a为静态库,是好多个.o合在一起,用于静态连接        .la为libtool自动生成的一些共享库,vi编辑查看,主要记录了一些配置信息.可以用如下命令查看*.la文件的格式   $file