hdu-1505 City Game(dp)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1505

题意,找出所给长方形中由F组成的长方形的最大面积*3。

思路:这个题的思路与1506的相同,不过有变化的是需要计算以长方形每行为底,每列所能达到的高度。即要计算M次的最大面积。首先需要处理计算到长方形每行时,每列所能达到的最大高度,此处可以用到前缀和思想,

if (s == 'F')
	dp[i][j] = dp[i - 1][j] + 1;
else
	dp[i][j] = 0;

dp[i][j]为第j列以第i行为底时的高度。

求每层的最大面积可以看我的另一篇博客,就不多说了。hdu-1506:https://blog.csdn.net/qq_42792291/article/details/85301472

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <set>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
int dp[1100][1100];
int main(){
	int t;
	scanf("%d", &t);
	while (t--){
		int m, n;
		scanf("%d%d", &n, &m);
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= n; i++){
			for (int j = 1; j <= m; j++){
				char s;
				scanf(" %c", &s);
				if (s == 'F')
					dp[i][j] = dp[i - 1][j] + 1;
				else
					dp[i][j] = 0;
			}
		}
		int MAX = -1;
		int l[1100];
		int r[1100];
		for (int i = 1; i <= n; i++){
			l[1] = 1;
			r[m] = m;
			for (int j = 2; j <= m; j++){
				int q = j;
				while (q > 1 && dp[i][j] <= dp[i][q - 1])
					q = l[q - 1];
				l[j] = q;

			}
			for (int j = m - 1; j >= 1; j--){
				int q = j;
				while (q < m&&dp[i][j] <= dp[i][q + 1])
					q = r[q + 1];
				r[j] = q;

			}
			for (int j = 1; j <= m; j++)
				MAX = max(MAX, dp[i][j] * (r[j] - l[j] + 1));
		}
		printf("%d\n", MAX * 3);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42792291/article/details/85318409
今日推荐