题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490
Parade
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 902 Accepted Submission(s): 396
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 west-east roads and
m+1 north-south roads. Of course, there are (n+1)×(m+1) 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 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
minutes. If it takes p minutes to pass a love-hate zone, we say the
length of that love-hate zone is p. 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.
Each test case consists of 2×n + 3 lines.
The first line contains three integers: n, m and k.(0<n<=100,0<m<=10000, 0<=k<=3000000)
The
next n+1 lines stands for n + 1 west-east roads in north to south
order. Each line contains m integers showing the welcome values of the
road’s m love-hate zones, in west to east order.
The last n+1
lines also stands for n + 1 west-east roads in north to south order.
Each line contains m integers showing the lengths (in minutes) of the
road's m love-hate zones, in west to east order.
7 8 1
4 5 6
1 2 3
1 1 1
1 1 1
1 1 1
0 0 0
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 104
#define M 10004 int L[N][M],R[N][M];
int v[N][M],t[N][M];
int n , m , k ;
int Q[M];
int f[M],sum[N][M],dp[N][M];
int main()
{
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",&v[i][j]);
for(int i = ; i <= n+ ; i++)
for(int j = ; j < m ;j++)
scanf("%d",&t[i][j]);
for(int i = ; i <= n+ ; i++)
{
sum[i][] = ;
for(int j = ; j <= m ;j++)
sum[i][j] = sum[i][j-] + v[i][j-];
}
for(int i = ; i <= n+ ; i++)
{
L[i][] = ;R[i][m] = m;
int cur = , id = ;
for(int j = ; j <= m ;j++){
cur+=t[i][j-];
while(cur>k) cur-=t[i][id++];
L[i][j] = id;
}
cur = ; id = m-;
for(int j = m- ; j>= ; j--){
cur+=t[i][j];
while(cur>k) cur -= t[i][id--];
R[i][j] = id+;
}
}
for(int i = ; i < m+ ; i++) dp[n+][i] = ;
for(int i = n ; i >= ; i--)
{
int head = , rear = ;
for(int j = ; j < m+ ; j++)
{
f[j] = dp[i+][j] - sum[i+][j];
while(rear < head && Q[rear] < L[i+][j]) rear++;
while(head > rear && f[j] >=f[Q[head-]]) head--;
Q[head++] = j;
dp[i][j] = max(dp[i+][j],sum[i+][j]+f[Q[rear]]);
}
head = , rear = ;
for(int j = m ; j>= ; j--)
{
f[j] = dp[i+][j] + sum[i+][j];
while(rear<head&&Q[rear]>R[i+][j]) rear++;
while(head>rear&&f[j]>=f[Q[head-]]) head--;
Q[head++] = j;
dp[i][j] = max(dp[i][j],f[Q[rear]]-sum[i+][j]);
}
}
int ans = ;
for(int i = ; i < m+ ;i++) ans = max(ans,dp[][i]);
printf("%d\n",ans);
}
return ;
}