例题 8-19 贩卖土地(Selling Land, ACM/ICPC NWERC 2010, UVa 12265)

原题链接:https://vjudge.net/problem/UVA-12265
分类:扫描法
备注:状态组织;单调栈

根据紫书上的描述,浅显易懂,一次就过了。首先存储好每个空地块最高高度。
关键点在于:①计算最长周长时,设当前列为 c 0 c_0 c0,左上角的列为 c c c,高度为 h h h,周长则为 2 ( c 0 − c + 1 + h ) 2(c_0-c+1+h) 2(c0c+1+h),因此周长由 c c c h h h来决定;②每一行从左往右遍历,单调栈来存储 h − c h-c hc,更小或者等于的左上角就不需要存储了,因为接下来的列如果最大高度更高,是不会影响已经存储的左上角,如果等于或者更低,显然 h − c h-c hc会更小,所以不需存储。

#include<bits/stdc++.h>
using namespace std;
int t,n,m,h[1005][1005],vis[4005],ans[4005];
char g[1005][1005];
struct Node{
	int col,h;
}a[1005];
int main(void){
	// freopen("in.txt","r",stdin);
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			scanf("%s",g[i]+1);
		for(int j=1;j<=m;j++){
			for(int i=1;i<=n;i++){
				if(g[i][j]=='#')h[i][j]=0;
				else h[i][j]=h[i-1][j]+1;
			}
		}
		memset(vis,0,sizeof(vis));
		int num=0;
		for(int i=1;i<=n;i++){
			int tot=0;
			for(int j=1;j<=m;j++){
				if(g[i][j]=='#'){
					tot=0; continue;
				}
				int col=j;
				while(tot&&h[i][j]<a[tot].h){
					col=a[tot].col;	
					tot--;
				}
				if(!tot||h[i][j]-col>a[tot].h-a[tot].col){
					tot++; 
					a[tot].h=h[i][j]; 
					a[tot].col=col;
				}
				int len=(j-a[tot].col+1+a[tot].h)*2;
				if(!vis[len])ans[num++]=len;
				vis[len]++;
			}
		}
		sort(ans,ans+num);
		for(int i=0;i<num;i++)printf("%d x %d\n",vis[ans[i]],ans[i]);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JILIN.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值