1427 - Parade (dp+单调队列)

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

\epsfbox{p4327.eps}

Input 

There are multiple test cases. Input ends with a line containing three zeros.


Each test case consists of n + 3 lines.

The first line contains three integers: n , m and k . (0 < n$ \le$100, 0 < m$ \le$10000, 0$ \le$k$ \le$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 mintegers showing the lengths (in minutes) of the road's m 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

题意:F城由n+1个横向路和m+1个竖向路组成。横向路每条路有时间和快乐值,现在每段横向路走的时间不能超过k,问从南到北能走的最大快乐值。

思路:dp,需要单调队列去优化,状态不难想,dp[i][j]表示走到(i,j)这个位置的最大快乐值,那么dp[i][j] = dp[i - 1][k] + sum[j] - sum[k](if时间满足),可是这样的话复杂度是o(n*m^2),需要用单调队列去优化,设func(x) = sum[x] - dp[i - 1][x];如此一来dp[i][j] = sum[j] - func(k); 用单调队列去维护func(k)的最小值即可。复杂度优化为o(n*m)。注意要从左往右和从右往左各一次,然后记录下较大的。

代码:

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define INF 0x3f3f3f3f
const int N = 105;
const int M = 10005;
int n, m, k, i, j, Happy[N][M], Time[N][M], dp[N][M][3], sumh[N][M][2], sumt[N][M][2];
deque<int> Q;

int func(int i, int j, int bo) {
	return sumh[i][j][bo] - dp[i - 1][j][2];
}
int solve() {
	int ans = -INF;
	for (i = 1; i <= n + 1; i++) {
		Q.clear();
		for (j = 0; j <= m; j++) {
			dp[i][j][0] = -INF;
			while (!Q.empty() && sumt[i][j][0] - sumt[i][Q.front()][0] > k) Q.pop_front();
			while (!Q.empty() && func(i, j, 0) <= func(i, Q.back(), 0)) Q.pop_back();
			Q.push_back(j);
			dp[i][j][0] = max(dp[i][j][0], sumh[i][j][0] - func(i, Q.front(), 0));
		}
		Q.clear();
		for (j = m; j >= 0; j--) {
			dp[i][j][1] = -INF;
			while (!Q.empty() && sumt[i][j][1] - sumt[i][Q.front()][1] > k) Q.pop_front();
			while (!Q.empty() && func(i, j, 1) <= func(i, Q.back(), 1)) Q.pop_back();
			Q.push_back(j);
			dp[i][j][1] = max(dp[i][j][1], sumh[i][j][1] - func(i, Q.front(), 1));
		}
		for (j = 0; j <= m; j++) {
			dp[i][j][2] = max(dp[i][j][0], dp[i][j][1]);
			if (i == n + 1)
				ans = max(ans, dp[i][j][2]);
		}
	}
	return ans;
}

int main() {
	while (~scanf("%d%d%d", &n, &m, &k) && n + m + k) {
		for (i = 1; i <= n + 1; i++) {
			for (j = 1; j <= m; j++) {
				scanf("%d", &Happy[i][j]);
				sumh[i][j][0] = sumh[i][j - 1][0] + Happy[i][j];
			}
			sumh[i][m + 1][1] = Happy[i][m + 1] = 0;
			for (j = m; j >= 0; j--) {
				sumh[i][j][1] = sumh[i][j + 1][1] + Happy[i][j + 1];
			}
		}
		for (i = 1; i <= n + 1; i++) {
			for (j = 1; j <= m; j++) {
				scanf("%d", &Time[i][j]);
				sumt[i][j][0] = sumt[i][j - 1][0] + Time[i][j];
			}
			sumt[i][m + 1][1] = Time[i][m + 1] = 0;
			for (j = m; j >= 0; j--) {
				sumt[i][j][1] = sumt[i][j + 1][1] + Time[i][j + 1];
			}
		}
		printf("%d\n", solve());
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Type-C Cable是一种符合Universal Serial Bus Type-C Cable and Connector Specification R2.0标准的USB-C数据线。\[1\]根据引用\[2\]的描述,由于非公开协议的限制,Intel TBT Gen3开始已经可以应用在USB-C Port上,所以对于TBT的相关Cable应该也会有兼容。而根据引用\[3\]的描述,谱瑞(Parade)收购了睿思科技(Fresco Logic),这可能意味着他们在USB这块有一定的技术积累。因此,Type-C Cable可能具备兼容TBT和USB 3.0的功能。 #### 引用[.reference_title] - *1* [【硬件】【USB】【Type-C】](https://blog.csdn.net/syjie19900426/article/details/109054926)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Usb Type-C 1.2 Usb Type-C Cable](https://blog.csdn.net/Strider_kong/article/details/126056566)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [PS188——谱瑞(Parade)推动的 Type-C扩展坞红海战略](https://blog.csdn.net/Type_C_Ken/article/details/124349798)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值