ACM-ICPC 2018 南京赛区网络预赛 - B. The writing on the wall

版权声明:是自己手打的没错 https://blog.csdn.net/Mr_Treeeee/article/details/82630539

https://nanti.jisuanke.com/t/30991

题意:

给你n*m的矩阵,给你k个黑点。问你有几个矩阵完全没有黑点。

POINT:

遍历右下角,再用O(m)的效率以这个右下角往左延伸能有多少个数。具体看代码注解。

#include <iostream>
#include <stdio.h>
#include <stack>
#include <algorithm>
using namespace std;
#define LL long long
const int N = 1e5+5;
const int M = 100+5;

int mp[N][M];
int up[M];
int main()
{
	int T;
	scanf("%d",&T);
	int cas=0;
	while(T--){
		int n,m,k;
		scanf("%d%d%d",&n,&m,&k);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++)
				mp[i][j]=up[j]=0;
		}
		for(int i=1;i<=k;i++){
			int x,y;scanf("%d%d",&x,&y);
			mp[x][y]=1;
		}
		LL ans=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(mp[i][j]) up[j]=i;
			}
			for(int j=1;j<=m;j++){//枚举右下角为(i,j)
				int h=i;//如果上面没有障碍,那么宽为1,长可以为(1,2,3,...,i)种,共i个。
				for(int k=j;k>=1;k--){
					h=min(h,i-up[k]);//如果上面有障碍,那么宽为(j-k+1),长可以为(1,2,...,i-up[i]).
					ans+=h;
				}

			}
		}
		printf("Case #%d: %lld\n",++cas,ans);
	}

	return 0;

}

猜你喜欢

转载自blog.csdn.net/Mr_Treeeee/article/details/82630539
今日推荐