CSP-J 2026 题解
CSP-J 2026
题目整体都比较简单,感觉难度大致和 2025 年差不多。
洛谷给的难度是 红橙黄绿,个人认为第四题应该处于 上位黄到下位绿 的区间。
注:笔者 AK 了 CSP-J 2026(详见游记),遂决定写一套题解记录一下。
目前贴的代码是赛后复现的,以后如果拿到了赛时代码会贴过来。
A.[CSP-J 2026] 旅游 / travel
按照题意 if 套 if 模拟即可。
注意右侧是 D,F 座,不是 D,E 座。
其实,考场上我并没有发现这一点,还好我的写法不关注这个。
Code
#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define inf 1e18
#define ls (2 * k)
#define rs (2 * k + 1)
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e6 + 5, M = 5e5 + 5;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
string a[2], b[2];
inline void solve(){
cin >> a[0] >> a[1] >> b[0] >> b[1];
string ans = "0000";
if(a[0] == b[0]){
ans[0] = '1';
if(a[1][0] == b[1][0] && a[1][1] == b[1][1]){
ans[1] = '1';
int ax = (a[1][2] - 'A' + 1);
int ay = (ax <= 3 ? 0 : 1);
int bx = (b[1][2] - 'A' + 1);
int by = (bx <= 3 ? 0 : 1);
if(ay == by){
ans[2] = '1';
if(ay == 1) ans[3] = '1';
else if(abs(ax - bx) == 1) ans[3] = '1';
}
}
}
cout << ans << "\n";
return ;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
while(tc--) solve();
return 0;
}
B. [CSP-J 2026] 回文串 / string
最优策略一定是只有两个位置不相同时,修改其中一个。
对于字符串长度较大的解法,本质就是对于前后缀的所有长度不同的块,再进行细化,统一成小块的长度。
可能有点难写,实现细节因做法不同而异,应该也可以左右维护两个指针,考场上觉得有巨量细节,就没有写。
这里的写法是分别做前后缀和,得到分割点,然后进行去重,二分寻找字符,细节相对而言比较少。
Code
#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define inf 1e18
#define ls (2 * k)
#define rs (2 * k + 1)
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e5 + 5, M = 4e5 + 5;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
char ch;
int n, tot, a[N], p[N];
int pre[N], suf[N], all[M], id[M];
inline void solve(){
cin >> n;
for(int i = 1;i <= n; i++){
cin >> a[i] >> ch;
p[i] = (ch - 'a' + 1);
}
for(int i = 1;i <= n; i++)
pre[i] = pre[i - 1] + a[i];
for(int i = n;i >= 1; i--)
suf[i] = suf[i + 1] + a[i];
for(int i = 1;i <= n; i++)
all[i] = pre[i], all[n + i] = suf[i];
sort(all + 1, all + 2 * n + 1);
tot = unique(all + 1, all + 2 * n + 1) - (all + 1);
for(int i = 1;i <= tot; i++){
int d = lower_bound(pre + 1, pre + n + 1, all[i]) - pre;
id[i] = p[d];
}
int ans = 0;
for(int i = 1;i <= tot; i++){
int to = tot - i + 1;
if(id[to] != id[i]) ans += (all[i] - all[i - 1]);
}
cout << ans / 2 << "\n";
return ;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
while(tc--) solve();
return 0;
}
C. [CSP-J 2026] 按位与 / and
宇宙超级无敌大典题。
由二进制 \(2 ^ n > \sum_{i = 0}^{n - 1} 2 ^ i\) 的性质可知,优先选高位一定最优。
从高到低,按位贪心即可。
Code
#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define inf 1e18
#define ls (2 * k)
#define rs (2 * k + 1)
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e5 + 5, M = 4e5 + 5;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
bool in[N];
int n, m, a[N];
inline void solve(){
cin >> n >> m;
for(int i = 1;i <= n; i++)
cin >> a[i], in[i] = true;
int ans = 0;
for(int k = 30;k >= 0; k--){
int now = (1ll << k), cnt = 0;
for(int i = 1;i <= n; i++){
if(in[i] == false) continue;
if((a[i] & now) > 0) cnt += 1;
}
if(cnt < m) continue;
ans += now;
for(int i = 1;i <= n; i++){
if(in[i] == false) continue;
if((a[i] & now) == 0) in[i] = false;
}
}
cout << ans << "\n";
return ;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
while(tc--) solve();
return 0;
}
D. [CSP-J 2026] 纸牌游戏 / card
这道题有点容易想偏,只去考虑最后一次出现的位置是难做的。
可以观察一下纸牌游戏的过程,就可以发现这样的结论:
第一张被打出的牌,会收走从第一次出现的位置一直到最后一次出现的位置中的所有牌,然后不断重复这个过程。
如果暴力地模拟去跳后缀的话,可以被卡成 \(O(n ^ 2)\),需要进一步优化。
令 \(f_i\) 表示跳到 \(i\) 时,即当前 \(i\) 是第一张牌的方案数。
每次转移 \(f_{lst_{a_i}} \leftarrow f_i\),复杂度 \(O(n)\)。
Code
#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define inf 1e18
#define ls (2 * k)
#define rs (2 * k + 1)
using namespace std;
const int mod = 1e9 + 7;
const int N = 5e5 + 5, M = 3e5 + 5;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int n, m, a[N], f[N], suf[M], ans[M];
inline void solve(){
cin >> n >> m;
for(int i = 1;i <= n; i++)
cin >> a[i], suf[a[i]] = i;
for(int i = 1;i <= n; i++){
f[i] = f[i] + 1;
int to = suf[a[i]] + 1;
if(to <= n) f[to] += f[i];
}
for(int i = 1;i <= n; i++)
ans[a[i]] += f[i];
for(int i = 1;i <= m; i++)
cout << ans[i] << " \n"[i == m];
return ;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
while(tc--) solve();
return 0;
}

浙公网安备 33010602011771号