警示后人&&数据是否需要改一改
  • 板块P1950 长方形
  • 楼主gongziwen
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/4/23 19:11
  • 上次更新2023/10/23 17:42:30
查看原帖
警示后人&&数据是否需要改一改
418739
gongziwen楼主2023/4/23 19:11
#include<cstdio>
#include<algorithm>
#include<iostream>
#define int long long
using std::max;
int n,size,m,h[2006],l[2006],r[2006],a[2006];
long long ans;
char ch[2006][2006];
signed main()
{
	std::cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) std::cin>>ch[i][j];
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
			if(ch[i][j]=='*') h[j]=0/*,printf("0 ")*/;
			else h[j]++/*,printf("%d ",h[j])*/;
		size=0;
		a[++size]=0;
		for(int j=1;j<=m+1;j++)
		{
			while(size>0&&h[j]<h[a[size]])
			{
				r[a[size]]=j;
				size--;
			}
			a[++size]=j;
		}
		size=0;
		a[++size]=0;
		for(int j=m;j>=0;j--)
		{
			while(size>0&&h[j]<=h[a[size]])
			{
				l[a[size]]=j;
				size--;
			}
			a[++size]=j;
		}
		for(int j=1;j<=m;j++) ans+=(r[j]-j)*1ll*(j-l[j])*h[j];
	}
	std::cout<<ans;
	return 0;
}

此代码能过

但是,

#include<cstdio>
#include<algorithm>
#define int long long
using std::max;
int n,size,m,h[2006],l[2006],r[2006],a[2006];
long long ans;
char ch[2006][2006];
signed main()
{
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++)
	{
		getchar();
		for(int j=1;j<=m;j++) ch[i][j]=getchar();
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
			if(ch[i][j]=='*') h[j]=0/*,printf("0 ")*/;
			else h[j]++/*,printf("%d ",h[j])*/;
//		puts("");
		size=0;
		a[++size]=0;
		for(int j=1;j<=m;j++)
		{
			while(size>0&&h[j]<h[a[size]])
			{
				r[a[size]]=j;
				size--;
			}
			a[++size]=j;
		}
  		while(size) r[a[size]]=m+1,size--;
		size=0;
		a[++size]=0;
		for(int j=m;j>=1;j--)
		{
			while(size>0&&h[j]<=h[a[size]])
			{
				l[a[size]]=j;
				size--;
			}
			a[++size]=j;
		}
  		while(size) l[a[size]]=0,size--;
		for(int j=1;j<=m;j++) ans+=(r[j]-j)*1ll*(j-l[j])*h[j];
	}
	printf("%lld",ans);
	return 0;
}

却WA10pts

可以看到两份代码出奇的相似,没错,它们算法部分完全一致,但是读入部分分别用的是cin和getchar。坑死人了,显然是在数据的人在某行后加了若干空格,使其读入空格才导致此结果的。

2023/4/23 19:11
加载中...