2026 CSP-J题解
[CSP-J 2026] 旅游
题意
给定四个字符串
若
在
在同一排的前提下,若
在同一侧的前提下,若
判断它们是否在同一节、同一排、同一侧、相邻。
思路
需要注意到字符集是
if (s1[2] == 'F') s1[2] = 'E';
if (s2[2] == 'F') s2[2] = 'E';
剩下的逻辑其实就是比较直接的分支判断,不过代码如果要写的简单一点,我们可以判断不正确的情况:
- 如果不在同一节车厢,就是
0000 - 否则如果不在同一排,就是
1000 - 否则如果不在同一侧,就是
1100 - 否则如果不相邻,就是
1110 - 否则就是
1111
代码
时间复杂度:
空间复杂度:
#include <bits/stdc++.h>
using namespace std;
int main() {
string c1, s1, c2, s2;
cin >> c1 >> s1 >> c2 >> s2;
if (s1[2] == 'F') s1[2] = 'E';
if (s2[2] == 'F') s2[2] = 'E';
if (c1 != c2) {
cout << "0000";
}
else if (s1[0] != s2[0] or s1[1] != s2[1]) {
cout << "1000";
}
else if (s1[2] <= 'C' and s2[2] > 'C' or s1[2] > 'C' and s2[2] <= 'C') {
cout << "1100";
}
else if (abs(s1[2]-s2[2]) != 1) {
cout << "1110";
}
else {
cout << "1111";
}
return 0;
}
[CSP-J 2026] 回文串
题意
给定一个字符串,问最少修改多少位置的字符,才能将其改为回文串。
字符串给出的形式形如:
其中
思路
容易想到的一个思路是暴力拼接出字符串之后,直接判断对应的字符是否相等,累加不相等的位置个数就是最终的答案,但这样只能拿到
正解考虑双指针。
我们考虑定义
- 若
c_l \neq c_r ,说明需要进行改变,需要改变多少个字符呢?比如本次的字符是3 \ c 和2 \ a ,显然是先改变两个字符,r 完成了配对,l 还剩一个字符进行配对。 - 若
c_l = c_r ,假设是3 \ c 和2 \ c ,说明有两个字符配对上了,可以削减为1 \ c 和0 \ c ,r 完成了配对,l 还剩一个字符需要配对。
这样就可以
代码
时间复杂度:
空间复杂度:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5+5;
struct P {
int x;
char c;
};
int n;
P a[N];
signed main() {
cin >> n;
for (int i=1; i<=n; i++) {
cin >> a[i].x >> a[i].c;
}
int l = 1, r = n, res = 0;
while (l < r) {
int t = min(a[l].x, a[r].x);
if (a[l].c != a[r].c) { // 不相等,需要改变t个字符
res += t;
}
a[l].x -= t;
a[r].x -= t;
// 完成配对了就往中间走
if (a[l].x == 0) l++;
if (a[r].x == 0) r--;
}
cout << res;
return 0;
}
[CSP-J 2026] 按位与
题意
给定
思路
容易发现暴力进行组合型枚举可以拿到
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
int n, m, res;
int a[N];
void dfs(int step, int cnt, int sum) {
if (step > n) {
if (cnt == m) res = max(res, sum);
return ;
}
dfs(step+1, cnt+1, sum&a[step]);
dfs(step+1, cnt, sum);
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n >> m;
for (int i=1; i<=n; i++) cin >> a[i];
if (m == 1) {
for (int i=1; i<=n; i++) res = max(res, a[i]);
cout << res << '\n';
return 0;
}
if (m == n) {
res = a[1];
for (int i=2; i<=n; i++) res &= a[i];
cout << res << '\n';
return 0;
}
if (m == 2) {
for (int i=1; i<=n; i++) {
for (int j=i+1; j<=n; j++) {
res = max(res, a[i]&a[j]);
}
}
cout << res << '\n';
return 0;
}
dfs(1, 0, INT_MAX);
cout << res;
return 0;
}
void dfs(int step, int last, int sum) {
if (step > m) {
res = max(res, sum);
return ;
}
// 关键剪枝,如果选了这个数字后,后面的数字全选都不足够拿到m个数字,直接不处理了
for (int i=last+1; i+m-step<=n; i++) {
dfs(step+1, i, sum & a[i]);
}
}
dfs(1, 0, INT_MAX);
正解需要考虑从二进制的角度来思考这个问题。
考过初赛的同学都知道,int 有
而按位与运算的规则是都为
如何执行这个策略呢?
可以从最高位开始枚举,统计有多少个数字这一位是
那么若
重复上述操作,只在可选的数字中枚举,看哪些位是可以变成
代码
时间复杂度:
空间复杂度:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5+5;
int n, m, a[N], res = 0;
int flag[N]; // 标记不可选数字,1表示不可选
signed main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);;
cin >> n >> m;
for (int i=1; i<=n; i++) {
cin >> a[i];
}
// 从高位往低位枚举
for (int i=30; i>=0; i--) {
int cnt = 0;
for (int j=1; j<=n; j++) {
if (flag[j]) continue; // 未选中的直接跳过
cnt += (a[j] >> i) & 1; // 统计这一位是1的个数
}
if (cnt < m) continue; // 不够选
res += (1 << i);
for (int j=1; j<=n; j++) {
if (flag[j]) continue;
if (((a[j] >> i) & 1) == 0) flag[j] = 1; // 这一位不是1的不能选
}
}
cout << res << '\n';
return 0;
}
[CSP-J 2026] 纸牌游戏
题意
给定
下面进行
依次把数字
比如此时的序列是
问对于
思路
首先考虑暴力模拟进行
void run(int x) {
// 从第x个数字开始模拟
stack<int> st; // 定义一个栈方便删末尾数字
memset(vis, 0, sizeof vis);
for (int i=x; i<=n; i++) {
while (vis[a[i]]) { // 如果数字已经存在了,就一直往前面删
vis[st.top()] = 0;
st.pop();
}
vis[a[i]] = 1;
st.push(a[i]);
}
// 记录最终留在栈中的数字,每个数字次数加1
while (st.size()) {
cnt[st.top()]++;
st.pop();
}
}
接着观察发现
- 若
m = 1 ,那么就是一个数字出现n 次,最后是必然会留下来的。 - 若
m=2 ,最终的序列必然是形如12,21,1,2 这样的形式,继续考虑什么时候会出现对应的形式。
我们注意到第
也就是说,对于last[],那么若本次操作的
if (m == 1) { // 特判1的情况
cout << n;
return 0;
}
if (m == 2) { // 特判2的情况
int last1 = -1, last2 = -1;
for (int i=1; i<=n; i++) {
if (a[i] == 1) last1 = i;
else last2 = i;
}
for (int i=1; i<=n; i++) {
if (a[i] == 1) {
cnt[1]++;
if (last1 != n) cnt[2]++;
}
else {
cnt[2]++;
if (last2 != n) cnt[1]++;
}
}
cout << cnt[1] << " " << cnt[2];
return 0;
}
这个特殊性质带给我们的一个启发是,第
设一个 last[x] 记录数字
这样是
void run(int x) {
// 从第x个数字开始模拟
int i = x;
while (i <= n) {
cnt[a[i]]++;
i = last[a[i]]+1;
}
}
但这样还是不够快的,时间复杂度是
我们发现对于同一种数字,不管起点是哪里,它都会跳到它最后一次的位置,而以这个数字作为起点的情况会发生
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5+5;
int n, m;
int a[N], last[N], cnt[N];
int c[N];
// last[i]记录数字最后一次出现的位置
void run(int x) {
// 从第x个数字开始模拟
int i = last[x];
while (i <= n) {
cnt[a[i]] += c[x]; // 增加对应的次数
i = last[a[i]]+1;
}
}
signed main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n >> m;
for (int i=1; i<=n; i++) {
cin >> a[i];
last[a[i]] = i;
c[a[i]]++;
}
for (int i=1; i<=m; i++) {
run(i);
}
for (int i=1; i<=m; i++) cout << cnt[i] << " ";
return 0;
}
接着往下考虑线性的做法,我们注意到如果第一次跳转是到
我们设
代码
时间复杂度:
空间复杂度:
#include<bits/stdc++.h>
#define qwq ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define int long long
using namespace std;
const int N = 5e5+5;
int n, m;
int a[N], last[N];
int c[N];
int f[N]; // f[i]表示第i个位置的经过次数,这里应该只统计每个数字最后一次出现的位置
// last[i]记录数字最后一次出现的位置
signed main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n >> m;
for (int i=1; i<=n; i++) {
cin >> a[i];
last[a[i]] = i;
c[a[i]]++;
}
for (int i=1; i<=m; i++) f[last[i]] = c[i]; // 初始状态
// 这里是必须正序循环,保证最前面的last[a[i]]先进行转移,也就是拓扑序。
for (int i=1; i<n; i++) {
if (i == last[a[i]]) {
int j = last[a[i+1]]; // 下一个跳转的位置
f[j] += f[i];
}
}
for (int i=1; i<=m; i++) cout << f[last[i]] << " ";
return 0;
}