寒假8双指针,BFS,图论

cpp 复制代码
#include<iostream>
#include<algorithm>
using namespace std;
int n, d, k;
int const  N = 1000010;
typedef pair<int, int> pii;
bool st[N];
int cnt[N];
pii logs[N];
int main()
{
	cin >> n >> d >> k;
	for (int i = 1;i <= n;i++)
	{
		cin >> logs[i].first >> logs[i].second;
	}
	sort(logs + 1, logs + 1 + n);
	for (int i = 1, j = 1;i <= n;i++)
	{
		cnt[logs[i].second]++;
		while (logs[j].first + d <= logs[i].first)
		{
			cnt[logs[j].second]--;
			j++;
		}
		if (cnt[logs[i].second] >= k)st[logs[i].second] = true;
	}
	for (int i = 0;i <= N;i++)
	{
		if (st[i])
		{
			cout << i << endl;
		}
	}
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
#include<queue>
#include<cstring>
int n, r, c;
int ans1[210][210];
char arr[210][210];
bool use[210][210];
typedef pair<int, int> PII;
int dx[] = { 0,0,-1,1 };
int dy[] = { -1,1,0,0 };
int bfs(int x, int y)
{
	PII pii;
	queue<PII>q;
	ans1[x][y] = 0;
	memset(use, 0, sizeof(use));
	pii.first = x;
	pii.second = y;
	q.push(pii);
	while (!q.empty())						    //RCCC
	{										    //RCCC
				;								//RCCC
		if (arr[q.front().first][ q.front().second] == 'E')
		{
			
			return ans1[q.front().first][q.front().second];
		}
		else
		{
			for (int i = 0;i < 4;i++)
			{
				if (use[q.front().first + dx[i]][q.front().second + dy[i]])continue;
				if (q.front().first + dx[i] > r || q.front().first + dx[i] < 1)continue;
				if (q.front().second + dy[i] > c || q.front().second + dy[i] < 1)continue;
				if (arr[q.front().first + dx[i]][ q.front().second + dy[i]] == '#')continue;
				pii.first = q.front().first + dx[i];
				pii.second = q.front().second + dy[i];
				ans1[q.front().first + dx[i]][q.front().second + dy[i]] = ans1[q.front().first ][q.front().second ] + 1;
				use[q.front().first + dx[i]][ q.front().second+dy[i]] = true;
				q.push(pii);
			}
		}
		q.pop();
	}
	
	return -1;

}
int main()
{
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		int sx, sy;
		cin >> r >> c;
		for (int j = 1;j <= r;j++)
		{
			for (int z = 1;z <= c;z++)
			{
				cin >> arr[j][z];
				if (arr[j][z] == 'S')
				{
					sx = j;
					sy = z;
				}
			}
		}
		int ans = bfs(sx, sy);
		if (ans == -1)
		{
			cout << "oop!" << endl;
		}
		else cout << ans << endl;
	}
	
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
int const N = 10010;
bool use[N];
int logs[N];
int main()
{
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		use[i] = true;
		cin >> logs[i];
	}
	int ans = 0;
	for (int i = 1;i <= n;i++)
	{
		if (!use[i])continue;
		use[i] = false;
		int flag = logs[i];
		while (flag != i)
		{
			use[flag] = false;
			flag = logs[flag];
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}
相关推荐
我是苏苏9 分钟前
C#高级:程序查询写法性能优化提升策略(附带Gzip算法示例)
开发语言·算法·c#
sali-tec1 小时前
C# 基于halcon的视觉工作流-章56-彩图转云图
人工智能·算法·计算机视觉·c#
黑岚樱梦5 小时前
代码随想录打卡day23:435.无重叠区间
算法
Kuo-Teng5 小时前
Leetcode438. 找到字符串中所有字母异位词
java·算法·leetcode
gihigo19986 小时前
MATLAB使用遗传算法解决车间资源分配动态调度问题
算法·matlab
墨染点香6 小时前
LeetCode 刷题【138. 随机链表的复制】
算法·leetcode·链表
却道天凉_好个秋6 小时前
目标检测算法与原理(一):迁移学习
算法·目标检测·迁移学习
兮山与7 小时前
算法24.0
算法
晓北斗NorSnow7 小时前
机器学习核心算法与学习资源解析
学习·算法·机器学习