1. Codeforces 1084 Div3 比赛全解析
作为一名算法竞赛老手,最近在Codeforces 1084 Div3比赛中取得了1700的表现分,虽然F题没能调出来有点遗憾,但整体发挥还算稳定。这次比赛涵盖了从简单到复杂的各类算法题型,特别适合有一定基础的选手进行练习。下面我将详细解析ABCDEFGH八道题目的解题思路和代码实现,希望能帮助大家提升算法竞赛水平。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 题目解析与代码实现
2.1 A. Eating Game
题目分析:
这道题的核心在于理解游戏规则。每轮所有人都要吃,只有最大值可以留到最后。由于可以指定从谁开始吃,所以所有最大值都可以成为最后的胜者。
解题思路:
- 找出数组中的最大值
- 统计最大值出现的次数
- 输出这个次数就是答案
代码实现:
cpp复制#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n+1);
for(int i=1; i<=n; i++) {
cin >> a[i];
}
int maxx = 0;
for(int i=1; i<=n; i++) {
maxx = max(maxx, a[i]);
}
int ans = 0;
for(int i=1; i<=n; i++) {
if(a[i] == maxx) {
ans++;
}
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1;
cin >> t;
while(t--) {
solve();
}
return 0;
}
注意事项:
- 注意数组下标从1开始还是从0开始
- 多个测试用例时记得初始化变量
- 时间复杂度O(n),空间复杂度O(n)
2.2 B. Deletion Sort
题目分析:
这道题考察对数组排序的理解。如果数组本来就是单调不减的,那么无法操作,能留下的就是n个原始数。否则,可以一直留着上升段,把其他的全删了,最后再把这个上升段删到只剩一个。
解题思路:
- 检查数组是否已经有序
- 如果有序,输出n
- 否则输出1
代码实现:
cpp复制#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n+1);
for(int i=1; i<=n; i++) {
cin >> a[i];
}
int ok = 1;
for(int i=2; i<=n; i++) {
if(a[i-1] > a[i]) {
ok = 0;
break;
}
}
if(ok) {
cout << n << endl;
} else {
cout << 1 << endl;
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1;
cin >> t;
while(t--) {
solve();
}
return 0;
}
注意事项:
- 注意等号情况的处理
- 边界条件检查很重要
- 时间复杂度O(n),空间复杂度O(n)
2.3 C. Specialty String
题目分析:
这道题需要每次选择相邻的两个相同字符消掉。对于这种消除再拼接再消除问题,可以考虑使用栈模拟。
解题思路:
- 使用栈来模拟消除过程
- 若当前栈顶元素和当前字符相同,就弹出栈顶
- 否则压入当前字符
- 最后判断栈是否为空
代码实现:
cpp复制#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
s = " " + s;
stack<int> stk;
for(int i=1; i<=n; i++) {
if(!stk.empty() && s[stk.top()] == s[i]) {
stk.pop();
} else {
stk.push(i);
}
}
