POJ 1964 City Game 解题报告 单调栈

POJ 1964 City Game 解题报告 单调栈

解题思路:单调栈。对于每一个点,计算以这个点为右下角的矩形的最大面积(不包括这个点所在的这一列)。代码里加了注释,请看。
在这里插入图片描述

#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<string.h>
#include<map>
#include<stack>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<fstream>
#include<sstream>
#include<set>
#pragma warning(disable:4996)
#define INF 0x3f3f3f3f
#define ll long long
#define PI acos(-1.0)
const int N = 100005;
const int maxn = 1e9;
using namespace std;
int n,m;
int h[1010][1010];
int a[1010],l[1010];
int top;
char ch[3];
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		memset(h, 0, sizeof(h));
		memset(a, 0, sizeof(a));
		memset(l, 0, sizeof(l));
		scanf("%d%d", &m,&n);
		for (int i = 1; i <= m; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				scanf("%s", ch);//用scanf单个输入字符,会录入空格和回车,这里用输入字符串的方式避免该问题
				if (ch[0] == 'F')
					h[i][j] = h[i - 1][j] + 1;//高度加1
				else
					h[i][j] = 0;
			}
		}
		int ans = 0;
		for (int i = 1; i <= m; i++)
		{
			top = 0;//top是新加在栈顶的元素位置
			int mx = 0;
			for (int j = 1; j <= n + 1; j++)//对于每一行都做一个单调栈
			{
				int tem = j;
				while (top > 0 && h[i][j] <= a[top])//满足这里的条件就先处理再入栈,不满足就直接入栈
				{
					mx = max(mx, (j - l[top]) * a[top]);
					tem = min(tem, l[top]);
					top--;
				}
				top++;//入栈
				a[top] = h[i][j];//a数组放高度
				l[top] = tem;//l数组放左边界,即这行第j个位置左边的连续F中最后一个F的位置
			}
			ans = max(ans, mx);
		}
		printf("%d\n", ans*3);
	}
}


发布了64 篇原创文章 · 获赞 0 · 访问量 1462

猜你喜欢

转载自blog.csdn.net/weixin_45566331/article/details/104410320