跳到正文

个人记录

2025.4.1 赛后总结

__DragonFly约 15 分钟
原文
文章信息与版本

文章信息

发布
2025年4月7日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 15 分钟

版本

  1. v1当前

T2 P5638 【CSGRound2】光骓者的荣耀

错误代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int a[N], s[N];
int n, k;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> k;
	int ans = 0;
	for (int i = 1; i < n; ++i)
	{
		cin >> a[i];
		ans += a[i];
	}
	if (k == 0)
	{
		cout << ans;
		return 0;
	}
	int maxx = 0, id = 0;
	for (int i = 1; i <= n; ++i)
	{
		int t1 = a[min(n, i + k)];
		int t2 = a[max(1, i - k)];
		if (t1 > maxx)
		{
			maxx = t1;
			id = min(n, i + k);
		}
		if (t2 > maxx)
		{
			maxx = t2;
			id = max(1, i - k);
		}
	}
	cout << ans - maxx;
	return 0;
}

错误原因

  • 1.我以为从1号城市传送到4号城市,还需要回头访问2号和3号城市。因为题目中所提到 ‘他可以不访问所有的城市’但终点必须是nn。
  • 2.处理问题:第24行因该是从0到n-1。

正确思路

可以用前缀和s[i]来表示到从0号城市i号城市需要多少时间。那么每次求出从i号城市到i+k号城市的所需时间,也就是s[i+k]-s[i],为什么不跟s[i-1]-s[i-k]比最大?因为a[i-k]~s[i-1]必定已经访问过了,为什么跟s[i-1]-s[i-k]比最大还能AC,因为s[i+k]-s[i]一定比s[i-1]-s[i-k]大。为什么不是s[i+k]-s[i-1]?因为a[i]已经访问过了。

node

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int a[N], s[N];
int n, k;

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> k;
	int ans = 0;
	for (int i = 2; i <= n; ++i)
	{
		cin >> a[i];
		s[i] = s[i - 1] + a[i];
	}
	int maxx = 0;
	for (int i = 1; i <= n - k; ++i)
	{
		maxx = max(maxx, s[i + k] - s[i]);
	}
	cout << s[n] - maxx;
	return 0;
}

T4 B3703 [语言月赛202301] 新年快乐

错误代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 405;
string a[N];
string s;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> s;
	int q;
	cin >> q;
	int len = s.size();
	while (q--)
	{
		int l, r;
		cin >> l >> r;
		string str = "";
		string mu = "";
		bool ok = 0;
		for (int i = l - 1; i <= r - 1; ++i)
		{
			str += s[i];
			mu += s[i];
			if (s[i] == 'a')
			{
				ok = 1;
			}
		}
		int lenstr = str.size();
		if (ok)
		{
			bool a = 1;
			for (int i = lenstr - 1; i >= 0; --i)
			{
				if (str[i] == 'a')
				{
					mu[i] = 'z';
					a = 1;
				}
				if (str[i] != 'a' && a == 1)
				{
					a = 0;
					mu[i] = char(str[i] - 1);
				}
			}
		}
		else
		{
			mu[lenstr - 1]--;
		}
		if (mu == str)
		{
			cout << "NULL\n";
			cout << "Happy Chinese New Year!\n";
			continue;
		}
		int cnt = 0;
		for (int i = 0; i < len; ++i)
		{
			if (i + (r - l + 1) >= len) continue;
			cnt++;
			a[cnt] = "";
			for (int j = i, k = l; k <= r; ++j, ++k)
			{
				a[cnt] += s[j];
			}
		}
		sort(a + 1, a + 1 + cnt);
		bool flag = 0;
		for (int i = 1; i <= cnt; ++i)
		{
			if (a[i] == mu)
			{
				cout << a[i] << "\n";
				flag = 1;
				break;
			}
		}
		if (!flag)
		{
			if (mu == str)
			{
				cout << "NULL\n";
			}
			else
			{
				cout << mu << "\n";
			}
			cout << "Happy Chinese New Year!\n";
		}
		else
		{
			cout << "Happy New Year!\n";
		}
	}
	return 0;
}

错误原因

  • 1.求排在字典序前的错误,如果str的字符串的末尾没有a,那么他就不会把末尾不是a的往前移,所以a初始化为1。如果末尾为a,a=1,如果进第2个if(str[i] != ‘a’ && a == 1),那么末尾的a会先变成z,又变成y,所以因写为else if(str[i] != ‘a’ && a == 1),并立马break,因为已经改好了。
  • 2.截取字符串错误,if (i + (r - l + 1) >= len) continue;因改为if (i + (r - l + 1) - 1 >= len) continue;
  • 3.sort多余。

conde

C++
#include <bits/stdc++.h>
using namespace std;
string s;

bool check(string x)
{
    int len = x.size();
    for (int i = 0; i < len; ++i)
    {
        if (x[i] != 'a') return 0;
    }
    return 1;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> s;
	int q;
	cin >> q;
	int len = s.size();
	while (q--)
	{
		int l, r;
		cin >> l >> r;
		string str = "";
		string mu = "";
		bool ok = 0;
		for (int i = l - 1; i <= r - 1; ++i)
		{
			str += s[i];
			mu += s[i];
			if (s[i] == 'a')
			{
				ok = 1;
			}
		}
        if (check(str))
        {
            cout << "NULL\nHappy Chinese New Year!\n";
            continue;
        }
		int lenstr = str.size();
		if (ok)
		{
			bool a = 1;
			for (int i = lenstr - 1; i >= 0; --i)
            {
                if (str[i] == 'a')
                {
                    mu[i] = 'z';
                    a = 1;
                }
                else if (str[i] != 'a' && a == 1)
                {
                    a = 0;
                    mu[i] = char(str[i] - 1);
                    break;
                }
            }
		}
		else
		{
			mu[lenstr - 1]--;
		}
        bool flag = 0;
		for (int i = 0; i + (r - l) < len; ++i)
		{
			string ans = "";
			for (int j = i, k = l; k <= r; ++j, ++k)
			{
				ans += s[j];
			}
            if (mu == ans)
            {
                flag = 1;
                cout << mu << "\n";
                break;
            }
		}
		if (!flag)
		{
			cout << mu << "\n";
			cout << "Happy Chinese New Year!\n";
		}
		else
		{
			cout << "Happy New Year!\n";
		}
	}
	return 0;
}

T5 P1434 [SHOI2002] 滑雪

题目思路

因为这道题的数据范围比较大,所以普通的dfs过不了,考虑记忆化搜索。

C++
状态表示:dfs(i, j)表示从i,j出发的最长斜坡距离。
状态转移方程:f[x][y] = max(f[x][y], dfs(x + 1, y); f[x][y] = max(f[x][y], dfs(x - 1, y)); f[x][y] = max(f[x][y], dfs(x, y + 1)); f[x][y] = max(f[x][y], dfs(x, y - 1));
递归结束条件或边界:
                  if (x < 1 || x > n || y < 1 || y > m) return 0;
                  if(f[x][y] != 1) return f[x][y];

code

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int f[N][N];
int a[N][N];
int n, m, ans;

int dfs(int x, int y)
{
	if (f[x][y]) return f[x][y];
	f[x][y] = 1;
	for (int i = 0; i < 4; ++i)
	{
		int nx = x + dx[i], ny = y + dy[i];
		if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
		if (a[nx][ny] >= a[x][y]) continue;
		f[x][y] = max(f[x][y], dfs(nx, ny) + 1);
	}
	return f[x][y];
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			cin >> a[i][j];
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			ans = max(ans, dfs(i, j));
		}
	}
	cout << ans;
	return 0;
}

T6 P5429 [USACO19OPEN] Fence Planning S

正确思路

题目大意

两头相互哞叫的奶牛属于同一哞网,现在直到每个奶牛的哞叫关系和每个奶牛的坐标,Farmer John 要建造一个四边与 x 轴和 y 轴平行的长方形围栏,每个长方形围栏至少一个哞网完全被围栏所包围,问满足他的要求的围栏的最小可能周长。

题目思路

因为数据比较大,所以我们有并查集来维护哞网的关系,然后求出每个哞网的最小的 x,y 坐标和最大的 x,y 坐标,统计在族长里,然后遍历每个族长,求出最小可能周长。

code

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 7;
int fa[N], x[N], y[N];
int minx[N], miny[N], maxx[N], maxy[N];
int n, m;

int get(int u)
{
	if (fa[u] == u) return u;
	return fa[u] = get(fa[u]);
}

void inti()
{
	for (int i = 1; i <= n; ++i) fa[i] = i;
	return ;
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	inti();
	for (int i = 1; i <= n; ++i) cin >> x[i] >> y[i];
	for (int i = 1; i <= n; ++i)
	{
		minx[i] = 1e18;
		miny[i] = 1e18;
		maxy[i] = -1e9;
		maxx[i] = -1e9;
	}
	for (int i = 1; i <= m; ++i)
	{
		int u, v;
		cin >> u >> v;
		int t1 = get(u), t2 = get(v);
		if (t1 != t2) fa[t1] = t2;
	}
	for (int i = 1; i <= n; ++i)
	{
		int t1 = get(i);
		minx[t1] = min(minx[t1], x[i]);
		miny[t1] = min(miny[t1], y[i]);
		maxx[t1] = max(maxx[t1], x[i]);
		maxy[t1] = max(maxy[t1], y[i]);
	}
	int ans = 1e18;
	for (int i = 1; i <= n; ++i)
	{
		if (fa[i] == i)
		{
			ans = min(ans, maxx[i] - minx[i] + maxy[i] - miny[i]);
		}
	}
	cout << ans * 2;
	return 0;
}
原文