跳到正文

个人记录

4.8 周二下午训练 - 东塘407

Moonlight_dreams约 13 分钟
原文
文章信息与版本

文章信息

发布
2025年4月12日
收录
2025年12月12日
更新
2026年11月22日
篇幅
约 13 分钟

版本

  1. v1当前

T3T3

P4995跳跳!P4995 跳跳!

思路思路

就是双指针,左指针从 0 开始,右指针从 nn 开始,然后whilewhile 循环,运行条件是左指针小于右指针。每次答案都加上 (a[i] - a[j]) * (a[i] - a[j]) (i 为左指针,j 为右指针),然后左指针减一,右指针加一。

错误代码错误代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 305;
int a[N];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	sort (a + 1 , a + n + 1);
	int maxn = n - 1 , minn = 2;
	int now = n , last = 0;
	int cnt = 0 , ans = 0;
	while (cnt < n)
	{
//		cout << now << " ";
		cnt++;
		ans += (a[now] - last) * (a[now] - last);
		last = now;
		if (cnt % 2 == 1)
		{
			now = minn;
			minn++;
		}
		else
		{
			now = maxn;
			maxn--;
		}
//		cout << ans << " " << maxn << " " << minn << endl;
	}
//	cout << endl;
	cout << ans;
	return 0;
}

错误原因错误原因

双指针错误。

AC代码如下AC 代码如下

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 305;
int a[N];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	sort (a + 1 , a + n + 1);
	int i = 0 , j = n;
	int ans = 0;
	while (i < j)
	{
		ans += (a[i] - a[j]) * (a[i] - a[j]);
		i++;
		ans += (a[i] - a[j]) * (a[i] - a[j]);
		j--;
	}
	cout << ans;
	return 0;
}

T4T4

P1141P1141 01迷宫01迷宫

思路思路

典型 dfsdfs 题~~

dfs函数dfs 函数

这道题数据范围比较大,需要加记忆化和预处理(不加的话会 TLETLE)

先加一个记忆化,然后枚举上下左右四个方向,并且判断当前位置满不满足要求,如果满足要求,就 dfsdfs。

主函数主函数

这里要加一个预处理,因为如果不加预处理,在要输出答案的时候,就会超时。

错误代码错误代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1005;
char a[N][N];
bool vis[N][N];
int n , m;
int f[N][N];
int dx[] = {0 , 0 , 1 , -1};
int dy[] = {1 , -1 , 0 , 0};
int cnt = 0;
void dfs(int x , int y)
{
	if (vis[x][y] == 1)
	{
		return ;
	}
	vis[x][y] = 1; 
	cnt++;
	for (int i = 0; i < 4; i++)
	{
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx <= 0 || ny <= 0 || nx > n || ny > n || a[nx][ny] == a[x][y] || vis[nx][ny] == 1)
		{
			continue;
		}
		dfs(nx , ny);
	}
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			cin >> a[i][j];
		}
	}
	while (m--)
	{
		int x , y;
		cin >> x >> y;
		memset(vis , 0 , sizeof vis);
		dfs(x , y);
		cout << cnt << endl;
		cnt = 0;
	}
	return 0;
}
错误原因错误原因

没加预处理和记忆化导致超时。

AC代码如下AC 代码如下

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n , m;
char a[N][N];
int dx[] = {1 , -1 , 0 , 0};
int dy[] = {0 , 0 , 1 , -1};
int vis[N][N] , s[N * N];
int cnt = 0;
void dfs(int x , int y , char z)
{
	vis[x][y] = cnt;
	for (int i = 0; i < 4; i++)
	{
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (vis[nx][ny] != 0) 
		{
			continue;
		}
		if (nx < 1 || ny < 1 || nx > n || ny > n) 
		{
			continue;
		}
		if (a[nx][ny] == z) 
		{
			continue;
		}
		dfs(nx , ny , a[nx][ny]);
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			cin >> a[i][j];
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			if (vis[i][j] == 0)
			{
				cnt++;
				dfs(i , j , a[i][j]);
			}
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			s[vis[i][j]]++;
		}
	}
	while (m--)
	{
		int x , y;
		cin >> x >> y;
		cout << s[vis[x][y]] << endl;
	}
	return 0;
}

T5T5

P11380[GESP202412八级]排队P11380 [GESP202412 八级] 排队

思路思路

这是一道并查集的题。

我们主要是来判断有没有环(如果有情况是 aa 要站在 bb 前面并且 bb 也要站在 aa 前面就可以说是有环)。

然后就找联通块,最后输出联通块个数。

当然,如果有环,就直接输出 0,不用找联通块。

AC代码如下AC 代码如下

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5 , MOD = 1e9 + 7;
int fa[N] , l[N] , r[N];
set <pair<int , int> >st;
int get(int x)
{
	if (x == fa[x])
	{
		return x;
	}
	return fa[x] = get(fa[x]);
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n , m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		fa[i] = i;
	}
	while (m--)
	{
		int u , v;
		cin >> u >> v;
		if (st.count({u , v}))
		{
			continue;
		}
		st.insert({u , v});
		int t1 = get(u) , t2 = get(v);
		if (r[u] != 0 || l[v] != 0 || t1 == t2)
		{
			cout << 0 << endl;
			return 0;
		}
		r[u] = v;
		l[v] = u;
		fa[t1] = t2;
	}
	int cnt = 0;
	for (int i = 1; i <= n; i++)
	{
		if (fa[i] == i)
		{
			cnt++;
		}
	}
	int ans = 1;
	for (int i = 1; i <= cnt; i++)
	{
		ans = ans * i % MOD;
	}
	cout << ans;
	return 0;
}

T6T6

P1205[USACO1.2]方块转换TransformationsP1205 [USACO1.2] 方块转换 Transformations

思路思路

这就一纯纯大模拟!!!

①① 旋转90°旋转90°

先造一个临时数组 tmptmp,然后让 tmp[i][j] = b[n - j + 1][j](b数组为原来的数组)。

②② 旋转180°旋转180°

就两次旋转90°。

③③ 旋转270°旋转270°

就三次旋转90°。

④④ 反射反射

tmp数组为临时数组,那么tmp[j][i] = b[n - i + 1][j]。

⑤⑤ 组合组合

先翻转,再尝试 1 ~ 3。

AC代码如下AC 代码如下

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 15;
char a[N][N] , ans[N][N] , b[N][N];
int n;
bool check(char b[][N])
{
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			if (b[i][j] != ans[i][j])
			{
				return 0;
			}
		}
	}
	return 1;
}
void xuan(char b[][N] , char c[][N])
{
	char tmp[N][N];
	for (int j = 1; j <= n; j++)
	{
		for (int i = 1; i <= n; i++)
		{
			tmp[i][j] = b[n - j + 1][i];
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			c[i][j] = tmp[i][j];
		}
	}
}
void fan(char b[][N] , char c[][N])
{
	char tmp[N][N];
	for (int j = 1; j <= n; j++)
	{
		for (int i = 1; i <= n; i++)
		{
			tmp[i][j] = b[i][n - j + 1];
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			c[i][j] = tmp[i][j];
		}
	}
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			cin >> a[i][j];
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			cin >> ans[i][j];
		}
	}
	xuan(a , b);
	if (check(b) == 1)
	{
		cout << 1;
		return 0;
	}
	xuan(b , b);
	if (check(b) == 1)
	{
		cout << 2;
		return 0;
	}
	xuan(b , b);
	if (check(b) == 1)
	{
		cout << 3;
		return 0;
	}
	fan(a , b);
	if (check(b) == 1)
	{
		cout << 4;
		return 0;
	}
	xuan(b , b);
	if (check(b) == 1)
	{
		cout << 5;
		return 0;
	}
	xuan(b , b);
	if (check(b) == 1)
	{
		cout << 5;
		return 0;
	}
	xuan(b , b);
	if (check(b) == 1)
	{
		cout << 5;
		return 0;
	}
	if (check(a) == 1)
	{
		cout << 6;
		return 0;
	}
	cout << 7;
	return 0;
}
原文