1015E2 通用思想 线段的表示

题目链接:

https://codeforces.com/problemset/problem/1015/E2

这样的题目大概是我现阶段能力的极限了吧

第一步,统计每一个点在水平和竖直方向上能双向延申的最长长度

第二步,统计每一个点在四个方向上能延伸的最长长度(取min)

第三部,用到一个技巧,就是一个线段[le,ri]可以表示为2个点,即从le开始,从ri+1结束

想到之前做到过的一个题,有很多条线段[le,ri],给出a,b,求在[a,b]范围内有有多少个点被奇数条线段覆盖

怎么做,有一点点技巧,记录每条线段的le和ri+1,弄个数组a[max(ri)],遍历,每个a[le]++,每个a[ri+1]--;

再统计出前缀和L[max(ri)],再从a遍历到b,中途有多少个a[i]&1==1的,就有多少个点被奇数条线段覆盖

联想:其实用到这种思想的,我算是做过两遍了,还遇到过牛B的一道题,这种思想是利用前缀和统计在数列上的线段,

这题只是统计有没有被覆盖,牛客竞赛里面还有一个更牛的,统计区间的交叉,不写博客我还差点忘了

https://ac.nowcoder.com/acm/problem/22141

意思就是给出很多线段,求从哪条线段开始,出现了线段的交叉,随机加前缀和就能过:

代码: 

//Problem:
//Date:
//Skill:
//Bug:
/////////////////////////////////////////Definations/////////////////////////////////////////////////
//循环控制
#define CLR(a) memset((a),0,sizeof(a))
#define F(i,a,b) for(int i=a;i<=int(b);++i)
#define F2(i,a,b) for(int i=a;i>=int(b);--i)
#define RE(i,n)  for(int i=0;i<int(n);i++)
#define RE2(i,n) for(int i=1;i<=int(n);i++)
//输入输出
//#define INC(c) do{scanf("%c",&c);}while(isspace(c))
//#define ON cout<<endl
#define PII pair<int,int>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long llinf = 0x3f3f3f3f3f3f3f3f;
////////////////////////////////////////Options//////////////////////////////////////////////////////
typedef long long ll;
#define stdcpph
#define CPP_IO

#ifdef stdcpph
#include<bits/stdc++.h>
#else
#include<ctype.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<algorithm>
#include<functional>
#ifdef CPP_IO
#include<iostream>
#include<iomanip>
#include<string>
#else
#include<stdio.h>
#endif
#endif
////////////////////////////////////////Basic Functions//////////////////////////////////////////////
template<typename INint>
inline void IN(INint &x)
{
	x = 0; int f = 1; char c; cin.get(c);
	while (c<'0' || c>'9') { if (c == '-')f = -1; cin.get(c); }
	while (c >= '0'&&c <= '9') { x = x * 10 + c - '0'; cin.get(c); }
	x *= f;
}
template<typename INint>
inline void OUT(INint x)
{
	if (x > 9)OUT(x / 10);
	cout.put(x % 10 + '0');
}
////////////////////////////////////////Added Functions//////////////////////////////////////////////
const int maxn = int(1e3+4);
int n, m;
int H[maxn][maxn], S[maxn][maxn];
//int L[maxn][maxn], To[maxn][maxn];
int used[maxn][maxn];
char ma[maxn][maxn];
void pr()
{
	int cnt(0);
	RE2(i, n)RE2(j, m)if (H[i][j])++cnt;
	cout << cnt << endl;
	RE2(i, n)
	{
		RE2(j, m)
		{
			if (H[i][j])
			{
				cout << i << ' ' << j << ' ' << H[i][j] << endl;
			}
		}
	}
}
////////////////////////////////////////////Code/////////////////////////////////////////////////////
int main()
{
	//freopen("C:\\Users\\VULCAN\\Desktop\\data.in", "r", stdin);
	int T(1), times(0);
#ifdef CPP_IO// CPP_IO
	std::ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	//cin >> T;
#else
	//IN(T);
#endif 
	/////////////////////////////////////////////////////////////////////////////////////////////////
	while (++times, T--)
	{
		cin >> n >> m; RE2(i, n)RE2(j, m)cin >> ma[i][j];
		RE2(i, n)
		{
			int l = 0;
			RE2(j, m+1)
			{
				if (ma[i][j] !='*')
					if (l == 0)
						continue;
					else
					{
						for (int k = l; k <= j - 1; ++k)H[i][k] = min(k - l, j - 1 - k);
						l = 0;
					}
				else
					if (l == 0)l = j;
					else continue;
			}
		}	
		RE2(j, m)
		{
			int l = 0;
			RE2(i, n+1)
			{
				if (ma[i][j]!='*')
					if (l == 0)continue;
					else
					{
						for (int k = l; k <= i - 1; ++k)S[k][j] = min(k - l, i - 1 - k); l = 0;
					}
				else
					if (l == 0)l = i;
					else continue;
			}
		}

		RE2(i, n)RE2(j, m)H[i][j] = min(H[i][j], S[i][j]);
		bool flag(1);
		RE2(i, n)
		{
			int cor[maxn] = { 0 };
			RE2(j, m)if (H[i][j])++cor[j - H[i][j]], --cor[j + H[i][j] + 1];
			RE2(j, m)cor[j] = cor[j - 1] + cor[j];
			RE2(j, m)if (cor[j])used[i][j] = 1;
		}
		RE2(j, m)
		{
			int cor[maxn] = { 0 };
			RE2(i, n)if (H[i][j])++cor[i - H[i][j]], --cor[i + H[i][j] + 1];
			RE2(i, n)cor[i] = cor[i - 1] +cor[i];
			RE2(i, n)if (cor[i])used[i][j] = 1;
		}
		RE2(i,n)RE2(j,m)
			if (!used[i][j] && ma[i][j] == '*')
			{
				flag = 0; break;
			}
		if (!flag)cout << "-1" << endl;
		else pr();
	}
	///////////////////////////////////////////////////////////////////////////////////////////////////
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41046771/article/details/88023916