跳到正文

个人记录

7.26 day02 总结

Eric140306约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年7月26日
更新
2026年7月27日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

7.26 day02 总结

得分情况与用时

T1: 100分,15分钟

T2: 90分,暴力解,15分钟

T3: 100分,25分钟

T4: 60分,“不那么暴力的暴力解”,35分钟

T5: 52分,暴力解,30分钟

T1题目解析

题意

当连续 n 天每天收到 n 枚金币后, 骑士会在之后的连续 n + 1 天, 每天收到 n + 1 枚金币。

求k天里,骑士共获得了多少金币

思路

分阶段枚举,cnt记录连续几天拿几金币,sum每次加cnt * cnt

如果剩余天数不够cnt天,sum加剩余天数 * cnt

代码

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int k, sum, cnt = 1;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> k;
	for(int i = 1; i <= k; i += cnt) {
		if(i <= k) sum += cnt * cnt;
		cnt++;
		if(i + cnt > k) sum += cnt * (k - i);
	}
	cout << sum;
	return 0;
}

个人总结

很简单,但是一开始没考虑到特判要提前

T2题目解析

题意

要在 L ~ R 中选择一个数, 使这个数对n取mod后的结果最大, 求这个最大的结果

思路

如果答案可以是 n - 1 那么糖数一定是 n的倍数 - 1

所以当 l / n = r / n 时,输出 n - 1

否则输出 r % n

代码

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int n, l, r;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> l >> r;
	if(l / n != r / n) cout << n - 1;
	else cout << r % n; 
	return 0;
}

个人总结

这道题主要是数学的运用,看来数学与编程之间的关联很深呢。学好变成的同时也要学好数学

T3题目解析

题意

共n个苹果,每隔两个苹果,拿走一个苹果

求多少天能拿完所有苹果、第几天能拿走第n个苹果

思路

每次会拿走 (cnt - 1) / 3 + 1 个苹果

如果(cnt - 1) % 3 = 0 说明第n个苹果被拿走了

代码

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int n, cnt, ans1 = 1e18, ans2;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n;
	cnt = n;
	for(int i = 1; i <= n; i++) {
		if((cnt - 1) % 3 == 0) ans1 = min(ans1, i);
		cnt -= (cnt - 1) / 3 + 1;
		ans2++;
		if(cnt == 0) break;
	}
	cout << ans2 << " " << ans1;
	return 0;
}

个人总结

这道题的数学思路比较简单,没有遇到什么困难

T4题目解析

题意

给出 k 个 n、d、e

求满足n = q * p , e * d = (q - 1) * (p - 1) + 1的 p 和 q;

思路

根据 n = q * p, 可以推导出 n - e * d + 2 = q + p

因为和一定差小积大,可以二分枚举出 q

代码

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int k;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> k;
	while(k--) {
		int n, d, e, p, q;
		cin >> n >> d >> e;
		int m = n - e * d + 2, f = 0;
		int l = 0, r = m / 2 + 1;
		while(l + 1 < r) {
			p = (l + r) / 2;
			q = m - p;
			if(p * q >= n) r = p;
			else l = p;
		}
		p = r;
		q = n / r;
		if(m == p + q && p * q == n) cout << p << " " << q << endl;
		else cout << "NO" << endl;
	}
	return 0;
}

个人总结

写完暴力解看到大样例超时,一心就想优化,却怎么也找不到突破口。二分真的很有用!!!

T5题目解析

题意

每轮两种操作:

将第x项替换成v

求排序后原本的第x项现在的位置

思路

保持数组是有序的

用桶标记原本第x个元素现在的位置,每次操作1就重新维护桶和数组。

当元素变大时,更换后面元素的顺序

反之,更换前面元素的顺序

代码

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
struct node {
	int a, id;
};
bool cmp(node x, node y) {
	if(x.a != y.a) return x.a < y.a;
	else return x.id < y.id;
}
int n, q, tong[200005];
node w[200005];
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> q;
	for(int i = 1; i <= n; i++) {
		cin >> w[i].a;
		w[i].id = i;
	}
	sort(w + 1, w + n + 1, cmp);
	for(int i = 1; i <= n; i++) tong[w[i].id] = i;
	while(q--) {
		int op, x, v;
		cin >> op;
		if(op == 1) {
			cin >> x >> v;
			int idx = tong[x];
			w[idx].a = v;
			while(idx > 1 && cmp(w[idx], w[idx - 1])) {
				swap(w[idx], w[idx - 1]);
				idx--;
			}
			while(idx < n && cmp(w[idx + 1], w[idx])) {
				swap(w[idx], w[idx + 1]);
				idx++;
			}
			for(int i = 1; i <= n; i++) tong[w[i].id] = i;
		}
		else {
			cin >> x;
			cout << tong[x] << endl;
		}
	}
	return 0;
}

个人总结

这道题利用了操作1很少的性质,这也教会了我一个经验:要利用好题目给出的每一个数据范围和性质。

整场考试总结

第1题其实在细节处理上碰上了一些小问题,但是在考场上也要能处理这种问题。

第2、3、4题主要是数学的应用,要掌握规律、推导公式,当然,第4题也有二分的灵活运用。

第5题只能说:要有明察秋毫的洞察力哇!

原文