寒假集训做题分享(5)

今天状态也不太好,晚上争取多学点,上午主要做了一道BFS题

[ABC311D] Grid Ice Floor

这个题主要考点是bfs的队列的运用,队列不熟这个题真的不太好想,而且while用的还

是不够熟练,总是不会很快想起while的使用,而且这个题使用两个数组,我好像没有

找出一个数组的做法,遗憾。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 5e6 + 10;
const int M = 10100;
int maxx = -1e18;
int minn = 1e18;
int cnt, sum;
bool f[M][M];
int ans[M][M];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
char a[M][M];
int n, m;
queue<pair<int, int>> q;
void bfs()
{
	q.push({2, 2});
	f[2][2] = 1;
	ans[2][2] = 1;
	while (!q.empty())
	{
		auto t = q.front();
		q.pop();
		int i;
		for (i = 0; i < 4 ; i++)
		{
			int nx = t.first;
			int ny = t.second;
			while (a[nx + dx[i]][ny + dy[i]] == '.')
			{
				nx += dx[i];
				ny += dy[i];
				ans[nx][ny] = 1;
			}
			if (f[nx][ny] == 1)
				continue;
			f[nx][ny] = 1;
			q.push({nx, ny});
		}
	}
}
signed main()
{

	cin >> n >> m;
	int i, j;
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			cin >> a[i][j];
			if (a[i][j] == '.')
				cnt++;
		}
	}
	bfs();
//	cout << cnt << endl;
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
//			cout << f[i][j] << " ";
			if (ans[i][j] != 0)
				sum++;
		}
//		cout << endl;
	}
	cout << sum;
}
/*
6 6
######
#....#
#.#..#
#..#.#
#....#
######
*/

P8661 [蓝桥杯 2018 省 B] 日志统计

滑动窗口,排序,\(deque <int> q[N];\),这是关键,思路,实现都对了,就在这个点上没想到。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 1e5 + 10;
const int M = 10100;
int maxx = -1e18;
int minn = 1e18;
bool f[N];
int n, d, k, maxn = 0;
deque <int> q[N];
struct node
{
	int ts, id;
} a[N];

bool cmp(node x, node y)
{
	if (x.id == y.id)
		return x.ts < y.ts;
	return x.id < y.id;
}

signed main()
{
	cin >> n >> d >> k;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i].ts >> a[i].id;
		maxn = max(maxn, a[i].id);
	}
	sort(a + 1, a + n + 1, cmp);
//	for(int i=1;i<=n;i++)
//	{
//		cout<<a[i].ts<<" "<<a[i].id<<endl;
//	}
	for (int i = 1; i <= n; i++)
	{
		q[a[i].id].push_back(a[i].ts);
		while (!q[a[i].id].empty() && a[i].ts - q[a[i].id].front() >= d)
			q[a[i].id].pop_front();
		if (q[a[i].id].size() >= k)
			f[a[i].id] = 1;
	}
	for (int i = 0; i <= maxn; i++)
	{
		if (f[i] != 0)
			cout << i << "\n";
	}
	return 0;
}

[ABC107B] Grid Compression

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 5e6 + 10;
const int M = 10100;
int maxx = -1e18;
int minn = 1e18;
int cnt, sum;
bool f[M][M];
char a[M][M];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
string s[15];
int n, m, k;
signed main()
{
	cin >> n >> m;
	int i, j;
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			cin >> a[i][j];
		}
	}
	for (i = 1; i <= n; i++)
	{
		int p = 0;
		for (j = 1; j <= m; j++)
		{
			if (a[i][j] == '#')
			{
				p = 1;
			}
		}
		if (p == 0)
		{
			for (int l = 1; l <= m; l++)
			{
				if (a[i][j] != '#')
				{
					f[i][l] = 1;
				}
			}
		}
	}
	for (i = 1; i <= m; i++)
	{
		int p = 0;
		for (j = 1; j <= n; j++)
		{
			if (a[j][i] == '#')
			{
				p = 1;
			}
		}
		if (p == 0)
		{
			for (int l = 1; l <= n; l++)
			{
				f[l][i] = 1;
			}
		}
	}
//	for (i = 1; i <= n; i++)
//	{
//		for (j = 1; j <= m; j++)
//		{
//			cout << f[i][j] << " ";
//		}
//		cout << endl;
//	}
	for (i = 1; i <= n; i++)
	{
		int p = 0;
		for (j = 1; j <= m; j++)
		{
			if (f[i][j] == 0)
			{
				cout << a[i][j];
				p = 1;
			}
		}
		if (p == 1)
			cout << endl;
	}
}
posted @ 2024-07-05 17:40  ZhangDT  阅读(10)  评论(0)    收藏  举报