跳到正文

个人记录

2025.8.1

mc__bang约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年8月1日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 9 分钟

版本

  1. v1当前

T4 P2689 东南西北

题意描述:

在平面直角坐标系中给出两个点,求第一个点在T个时刻后要用多少步才能到第二个点。

错误原因:

没有考虑不能到达时输出-1的情况。

解题思路:

先求出两点之间的x轴与y轴之间的距离:

C++
long long xn = abs(xx1 - xx2), yn = abs(yy1 - yy2);
ans = xn + yn;

再输入风向,每输入一个,就将对应的桶加一:

C++
for (int i = 1; i <= n; i++){
	cin >> c[i];
	if (c[i] == 'E') cnte++;
	if (c[i] == 'S') cnts++;
	if (c[i] == 'W') cntw++;
	if (c[i] == 'N') cntn++;
}

判断需要的方向是否足够,足够时就输出答案,不够就输出-1:

代码
if (xx1 > xx2){
	if (cntw >= xn){
		s = 1;
	}
	else{
		cout << -1;
		return 0;
	}
}
if (xx1 < xx2){
	if (cnte >= xn){
		s = 1;
	}
	else{
		cout << -1;
		return 0;
	}
}
if (yy1 > yy2){
	if (cnts >= yn){
		s = 1;
	}
	else{
		cout << -1;
		return 0;
	}
}
if (yy1 < yy2){
	if (cntn >= yn){
		s = 1;
	}
	else{
		cout << -1;
		return 0;
	}
}

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
long long s, n, xx1, yy1, xx2, yy2, ans, cnte, cnts, cntw, cntn;
char c[51];
int main(){
	cin >> xx1 >> yy1 >> xx2 >> yy2 >> n;
	long long xn = abs(xx1 - xx2), yn = abs(yy1 - yy2);
	ans = xn + yn;
	for (int i = 1; i <= n; i++){
		cin >> c[i];
		if (c[i] == 'E') cnte++;
		if (c[i] == 'S') cnts++;
		if (c[i] == 'W') cntw++;
		if (c[i] == 'N') cntn++;
	}
	if (xx1 > xx2){
		if (cntw >= xn){
			s = 1;
		}
		else{
			cout << -1;
			return 0;
		}
	}
	if (xx1 < xx2){
		if (cnte >= xn){
			s = 1;
		}
		else{
			cout << -1;
			return 0;
		}
	}
	if (yy1 > yy2){
		if (cnts >= yn){
			s = 1;
		}
		else{
			cout << -1;
			return 0;
		}
	}
	if (yy1 < yy2){
		if (cntn >= yn){
			s = 1;
		}
		else{
			cout << -1;
			return 0;
		}
	}
	cout << ans;
	return 0;
}

T5 P6568 [NOI Online #3 提高组] 水壶

题目描述:

有n个容量无穷大的水壶,它们从1∼n编号,初始时i号水壶中装有a[i]单位的水。

给出一个操作次数上限,每次将任意一个水壶中的所有水倒到下一个水壶中。

最后需要给出任意一个水壶中的水的最大值。

错误原因:

没有思路,没写完。

解题思路:

先求出前缀和:

C++
for (int i = 1; i <= n; i++){
	sum[i] = sum[i - 1] + a[i];
}

再找区间和的最大值:

C++
for (int i = k + 1; i <= n; i++){
  su = sum[i] - sum[i - k - 1];
  maxn = max(maxn, su);
}

正确答案:

C++
#include<bits/stdc++.h>
using namespace std;
long long n, a[1000005], k, sum[1000005], su, maxn;
int main(){
	cin >> n >> k;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++){
		sum[i] = sum[i - 1] + a[i];
	}
	for (int i = k + 1; i <= n; i++){
		su = sum[i] - sum[i - k - 1];
		maxn = max(maxn, su);
	}
	cout << maxn;
	return 0;
}

P5594 【XR-4】模拟赛

题目描述:

有n人要按顺序打完m套模拟赛。如果每天有重复的题目,就只需要开一场。要求每天至少开几场。

错误原因:

没有考虑相同的情况。

解题思路:

枚举每一天要打哪些模拟赛,找到后当天总和加一:

C++
for(int k = 1; k <= s; k++){
	for (int i = 1; i <= m; i++){
		for (int j = 1; j <= n; j++){
			if (a[j][i] == k){
				ans[k]++;
				break;
			}
		}
	}
}

正确答案:

C++
#include<bits/stdc++.h>
using namespace std;
int n, m, s, q, ans[1005], a[1005][1005];
int main(){
	cin >> n >> m >> s;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= m; j++){
			cin >> a[i][j];
		}
	}
	for(int k = 1; k <= s; k++){
		for (int i = 1; i <= m; i++){
			for (int j = 1; j <= n; j++){
				if (a[j][i] == k){
					ans[k]++;
					break;
				}
			}
		}
	}
	for (int i = 1; i <= s; i++) cout << ans[i] << " ";
}

T7 P12176 [蓝桥杯 2025 省 Python B] 书架还原

题目描述:

有一个无序的序列,需要求出至少交换几次才能让它变得有序。

错误原因:

将把a[mp[i]]换到a[i]写成了把a[mp[i]]换到mp[i]。

解题思路:

遍历整个数组,如果当前位置的错误,就让他与正确的互换,并把总数加一。

C++
for (int i = 1; i <= n; i++){
	if (a[i] != i){
		mp[a[i]] = mp[i];
		swap(a[i], a[mp[i]]);
		ans++;
	}
}

正确答案:

C++
#include<bits/stdc++.h>
using namespace std;
int ans, n, a[1000005], mp[1000005];
int main(){
	cin >> n;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		mp[a[i]] = i;
	}
	for (int i = 1; i <= n; i++){
		if (a[i] != i){
			mp[a[i]] = mp[i];
			swap(a[i], a[mp[i]]);
			ans++;
		}
	}
	cout << ans;
	return 0;
}
原文