跳到正文

个人记录

5.6 周二下午训练 -- 东塘407 总结

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

文章信息

发布
2025年5月6日
收录
2025年12月12日
更新
2026年11月22日
篇幅
约 18 分钟

版本

  1. v1当前

成绩

T1T1T2T2T3T3T4T4T5T5T6T6
100100303000100100606000

T2T2

B3702[语言月赛202301]华小科的旅行开始了B3702 [语言月赛202301] 华小科的旅行开始了

思路思路

很简单的模拟。。。

我们找到结束的坐标 (i,j)(i,j),然后 whilewhile 循环,每一次都先输出当前位置,然后更新当前位置。

错误代码错误代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
int x[N][N] , y[N][N];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n , m , sx , sy;
	cin >> m >> n >> sx >> sy;
	int ex , ey;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			cin >> x[i][j] >> y[i][j];
			if (x[i][j] == 0 && y[i][j] == 0)
			{
				ex = i;
				ey = j;
			}
		}
	}
//	cout << ex << " " << ey << endl;
	int nx = 1 , ny = 1;
	while (x[nx][ny] != 0 && y[nx][ny] != 0)
	{
		cout << nx << " " << ny << endl;
		int fx = nx , fy = ny;
		nx = x[fx][fy];
		ny = y[fx][fy];
//		cout << nx << " " << ny << " " << x[nx][ny] << " " << y[nx][ny] << endl;
	}
	cout << nx << " " << ny << endl;
	return 0;
}

错误原因错误原因

每一次都是从 (Sx,Sy)(Sx , Sy) 开始旅行的,但我是从 (1,1)(1 , 1) 开始的…痛失70分┭┮﹏┭┮

AC代码如下AC 代码如下

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
int x[N][N] , y[N][N];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n , m , sx , sy;
	cin >> m >> n >> sx >> sy;
	int ex , ey;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			cin >> x[i][j] >> y[i][j];
			if (x[i][j] == 0 && y[i][j] == 0) // 找到结束的点
			{
				ex = i;
				ey = j;
			}
		}
	}
//	cout << ex << " " << ey << endl;
	int nx = sx , ny = sy;
	while (x[nx][ny] != 0 && y[nx][ny] != 0)
	{
		cout << nx << " " << ny << endl; // 输出答案
		int fx = nx , fy = ny;
		nx = x[fx][fy];
		ny = y[fx][fy]; // 更新当前位置
//		cout << nx << " " << ny << " " << x[nx][ny] << " " << y[nx][ny] << endl;
	}
	cout << nx << " " << ny << endl;
	return 0;
}

T3T3

B3681[语言月赛202211]PowerStripB3681 [语言月赛202211] Power Strip

思路思路

记忆化搜索……

  • dfsdfs 函数

    因为这道题数据范围比较大,所以我们需要用记忆化搜索来做。

    我们用 ff 数组来存答案,然后遍历所有跟当前点有关的点,累计答案。

  • 主函数

    我们通过题目给的图可以知道,其实输入的长度为 n−1n - 1 的序列其实是一棵树,所以我们输入后就可以利用 vectorvector 来建边存树。

    然后我们 dfsdfs,最后输出 fif_i。

AC代码如下AC 代码如下

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int u[N] , a[N] , f[N];
vector <int> g[N];
int dfs(int step)
{
	f[step] = a[step]; // 当前点至少会有 a[step] 这么多
	int len = g[step].size();
	for (int i = 0; i < len; i++) // 遍历所有跟当前点有关联的点
	{
		int u = g[step][i];
		f[step] += dfs(u); // 累计答案
	}
	return f[step];
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	for (int i = 2; i <= n; i++)
	{
		cin >> u[i];
		g[u[i]].push_back(i); // 建边
	}
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	dfs(1);
	for (int i = 1; i <= n; i++)
	{
		cout << f[i] << " "; // 输出答案
	}
	return 0;
}

T5T5

P1609最小回文数P1609 最小回文数

思路思路

我们分多种情况来讨论:

No.1No.1 这个字符串本身就是回文字符串

① 位数是偶数(以 ABCDEF 为例)

我们可以知道,如果样例给的是 ABCDEF,那么 ABCCBA 肯定最小回文数。但这里我们也要分几种情况:

  • ABCDEF << ABCCBA

    那么 ABCCBA 就是答案。

  • ABCDEF ≤\le ABCCBA

我们就取出 ABC,然后将 C 加一,答案字符串就为 AB(C+1)(C+1)BA。

但是这里有一个特例,就是如果 C 为9,那 C 加一就是 :,就出错了。我们可以加上一个判断,但又不能只加一个,因为有可能 B 也为9。所以我们加上一个循环,只要当前字符是 9,我们就判断下一个字符,否则,当前字符加一,退出循环,我们将这个修改后的字符串称为 GHI。

最后答案就是 GHIIHG

② 位数是奇数(以 ABCDEFG 为例)

我们可以知道,如果样例给的是 ABCDEFG,那么 ABCDCBA 肯定最小回文数。但这里我们也要分几种情况:

  • ABCDCBA << ABCDEFG

    答案就是 ABCDCBA。

  • ABCDCBA ≤\le ABCDEFG

    我们就让 D 加一,答案字符串就为 ABC(D+1)CBA。

    但是这里有一个特例,就是如果 D 为9,那 D 加一就是 :,就出错了。我们可以加上一个判断,但又不能只加一个,因为有可能 C 也为9。所以我们加上一个循环,只要当前字符是 9,我们就判断下一个字符,否则,当前字符加一,退出循环,我们将这个修改后的字符串称为 HIJK。

    最后答案就为 HIJKJIH。

No.2No.2 这个字符串本身不是回文字符串

与上面的同理。

No.3No.3还有一个特例

这个特例就是全是9的情况。如果全是9,我们就先输出一个1,然后输出 len−1len - 1 个 0(len 为字符串长度),最后再输出一个1。(可以自己推导)

错误代码错误代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
bool check(string s , int lens)
{
	string st = "";
	for (int i = lens - 1; i >= 0; i--)
	{
		st += s[i];
	}
	if (s == st)
	{
		return 1;
	}
	return 0;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	string s;
	cin >> s;
	int lens = s.size();
	if (check(s , lens) == 1)
	{
		string t = s.substr(0 , lens / 2);
		int lent = t.size();
		if (lens % 2 == 0)
		{
			t[lent - 1]++;
			cout << t;
			string st = "";
			for (int i = lent - 1; i >= 0; i--)
			{
				st += t[i];
			}
			cout << st;
		}
		else
		{
			cout << t;
			cout << char(s[lent] + 1);
			string st = "";
			for (int i = lent - 1; i >= 0; i--)
			{
				st += t[i];
			}
			cout << st;
		}
	}
	else
	{
		string t = s.substr(0 , lens / 2);
		int lent = t.size();
		if (lens % 2 == 0)
		{
//			t[lent - 1]++;
			if (t[0] <= s[lens - 1])
			{
				t[lent - 1]++;
//				cout << s[lens - 1] << " " << t[0] << " ";
			}
			cout << t;
			string st = "";
			for (int i = lent - 1; i >= 0; i--)
			{
				st += t[i];
			}
			cout << st;
		}
		else
		{
			cout << t;
			cout << char(s[lent] + 1);
//			cout << s[lent + 1];
//			cout << lent;
			string st = "";
			for (int i = lent - 1; i >= 0; i--)
			{
				st += t[i];
			}
			cout << st;
		}
	}
	return 0;
}

错误原因错误原因

没有判断9的情况。

90分代码90 分代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
bool check(string s , int lens)
{
	string st = "";
	for (int i = lens - 1; i >= 0; i--)
	{
		st += s[i];
	}
	if (s == st)
	{
		return 1;
	}
	return 0;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	string s;
	cin >> s;
	if (s == "0" || s == "1" || s == "2" || s == "3"  || s == "4"  || s == "5"  || s == "6"  || s == "7"  || s == "8" || s == "9") 
	{
		cout << 11;
		return 0;
	}
	int lens = s.size();
	bool flag = 0;
	for (int i = 0; i < lens; i++)
	{
		if (s[i] != '9')
		{
			flag = 1;
		}
	}
	if (flag == 0)
	{
		cout << 1;
		for (int i = 1; i <= lens - 1; i++)
		{
			cout << 0;
		}
		cout << 1;
		return 0;
	}
	if (check(s , lens) == 1)
	{
		string t = s.substr(0 , lens / 2);
		int lent = t.size();
		if (lens % 2 == 0)
		{
			for (int i = lent - 1; i >= 0; i--)
			{
				if (t[i] == '9')
				{
					t[i] = '0';
				}
				else
				{
					t[i]++;
					break;
				}
			}
			cout << t;
			string st = "";
			for (int i = lent - 1; i >= 0; i--)
			{
				st += t[i];
			}
			cout << st;
		}
		else
		{
			char x = s[lent];
			if (x == '9')
			{
				x = '0';
				for (int i = lent - 1; i >= 0; i--)
				{
					if (t[i] == '9')
					{
						t[i] = '0';
					}
					else
					{
						t[i]++;
						break;
					}
				}
			}
			else
			{
				x++;
			}
			cout << t;
			cout << x;
			string st = "";
			for (int i = lent - 1; i >= 0; i--)
			{
				st += t[i];
			}
			cout << st;
		}
	}
	else
	{
		string t = s.substr(0 , lens / 2);
		int lent = t.size();
//		cout << s.substr(lens / 2 , lens / 2) << endl;
		if (lens % 2 == 0)
		{
//			t[lent - 1]++;
			string str = t;
			reverse(str.begin() , str.end());
			if (str <= s.substr(lens / 2 , lens / 2))
			{
				for (int i = lent - 1; i >= 0; i--)
				{
					if (t[i] == '9')
					{
						t[i] = '0';
					}
					else
					{
						t[i]++;
						break;
					}
				}
//				cout << s[lens - 1] << " " << t[0] << " ";
			}
			cout << t;
			string st = "";
			for (int i = lent - 1; i >= 0; i--)
			{
				st += t[i];
			}
			cout << st;
		}
		else
		{
			char x = s[lent];
			if (x == '9')
			{
				x = '0';
				for (int i = lent - 1; i >= 0; i--)
				{
					if (t[i] == '9')
					{
						t[i] = '0';
					}
					else
					{
						t[i]++;
						break;
					}
				}
			}
			else
			{
				x++;
			}
			cout << t;
			cout << x;
			string st = "";
			for (int i = lent - 1; i >= 0; i--)
			{
				st += t[i];
			}
			cout << st;
		}
	}
	return 0;
}

实在调不出来了awa

T6T6

P11486「CfzRound5」MatarainenP11486 「Cfz Round 5」Mata rainen

思路思路

这道题什么情况下会输出 No 呢?我们画图分析后可以得知,如果有环,就输出 No。

我们考虑没有环的情况。我们找到一个点对 (sk,tk)(s_k , t_k),将它设为开头的,然后剩下的直接连边。此时我们就有若干个联通块,最后我们把这些联通块都串联起来。

AC代码如下AC 代码如下

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5 + 5;
int x[N] , y[N];
int fa[N];
int get(int x)
{
	if (x == fa[x])
	{
		return x;
	}
	return fa[x] = get(fa[x]);
}
void unite(int x , int y)
{
	x = get(x);
	y = get(y);
	if (x != y)
	{
		fa[x] = y;
	}
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n , m;
	cin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		cin >> x[i] >> y[i];
	}
	for (int i = 1; i <= n; i++)
	{
		fa[i] = i;
	}
	for (int i = 1; i <= m; i++)
	{
		int nx = get(x[i]);
		int ny = get(y[i]);
		if (nx != ny)
		{
			unite(nx , ny);
		}
		else
		{
			cout << "No";
			return 0;
		}
	}
	cout << "Yes" << endl;
	for (int i = 1; i <= m - 1; i++)
	{
		cout << x[i] << " " << y[i] << endl;
	}
	int pre = x[m];
	for (int i = 1; i <= n; i++)
	{
		if (get(i) == i && get(i) != get(x[m]))
		{
			cout << pre << " " << i << endl;
			pre = i;
		}
	}
	cout << pre << " " << y[m];
	return 0;
}
原文