ZZJC新生训练赛第九场题解
链接:https://ac.nowcoder.com/acm/contest/94229
密码:zzjcacm
A题
思路
重点在于题目操作蕴含的奇偶数关系,一个偶数可以和一个奇数一起删除,两个奇数可以一起删除。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
vector<int>ar(n);
int j=0,o=0;//j代表奇数的个数,o代表偶数的个数
for(int i=0;i<n;i++){
cin>>ar[i];
if(ar[i]&1){
j++;
}else{
o++;
}
}
if(o>j)cout<<max(0,o-j);
else{
if((j-o)&1){
cout<<1;
}else{
cout<<0;
}
}
}
B题
思路
只需把递归式子拆开来,等差数列求个和,每步操作%P防止超出精度就行。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
#define int long long
int t;
cin>>t;
int p = 998244353;
while(t--){
int a,x;
cin >> a >> x;
if(x==1){
cout<<a%p<<'\n';
continue;
}
cout<<(x*(x-1)/2)%p*a%p*a%p<<'\n';
}
}
C题
思路
排序贪心即可,找在x范围内最大的ai,和它前面共有几个数。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
cin>>n>>m;
vector<int>a(n);
for(auto &x:a){
cin>>x;
}
sort(a.begin(),a.end());
int cnt=0;
int maxn=0;
for(auto x:a){
if(x<=m){
cnt++;
maxn=x;
}
}
cout<<cnt<<" "<<m-maxn<<'\n';
}
D题
思路
BFS模板题,dxdy里存四个方向,宽度优先搜索,每一个向外扩展,最先搜到的就是最少的步数。
代码
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int n, m;
char g[N][N];
int d[N][N];
int bfs()
{
queue<PII> q;
memset(d, -1, sizeof d);
d[0][0] = 0;
q.push({0, 0});
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while (q.size())
{
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i ++ )
{
int x = t.first + dx[i], y = t.second + dy[i];
if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] !=g[t.first][t.second] && d[x][y] == -1)
{
d[x][y] = d[t.first][t.second] + 1;
q.push({x, y});
}
}
}
return d[n - 1][m - 1];
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
cin >> g[i][j];
cout << bfs() << endl;
return 0;
}
E题
思路
上一题的构造方法有很多,重点在于构造出一条满足条件的路径,且把其他路堵住,代码很shit。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
cin>>n>>m;
char g[n+1][m+1];
int cnt=0;
bool flag=false;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(i==n){
g[i][j]='a'+cnt;
if(j%2!=0)cnt++;
}
else {
g[i][j]='a'+cnt;
if(j%2==0)cnt++;
}
if(cnt==26)cnt=0;
}
if(cnt==26)cnt=0;
}
if(g[n-1][2]==g[n][2]){
int p=g[n-1][2]-'a';
p++;
if(p==26)p=0;
g[n][3]='a'+p;
p++;
if(p==26)p=0;
g[n-1][3]='a'+p;
p++;
for(int i=4;i<=m;i++){
g[n-1][i]='a'+p;
p++;
if(p==26)p=0;
}
g[n][m]='a'+p;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<g[i][j];
}
cout<<'\n';
}
}
F题
思路
签到,只要看每个后面是否有大写,有大写代表这里需要一次操作,答案++。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string ss;
cin>>ss;
int cnt=0;
for(int i=0;i<ss.size()-1;i++){
if(ss[i+1]<='Z'&&ss[i+1]>='A'){
cnt++;
i++;
}
}
cout<<cnt<<"\n";
}
G题
思路
依旧是排序贪心,只要剩余数量大于k个就取就行。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
#define int long long
int n,k;
cin>>n>>k;
vector<int>ar(n+1);
int sm=0;
for(int i=1;i<=n;i++){
cin>>ar[i];
sm+=ar[i];
}
sort(ar.begin()+1,ar.end());
int sum=0;
int cnt=0;
if(sm%k==0){
cout<<0;
return 0;
}
for(int i=n;i>0;i--){
sum+=ar[i];
cnt++;
if(sum>sm-sm/k*k){
break;
}
}
cout<<cnt;
}
H题
思路
利用set判断是否这位数出现过,把各个特殊情况判一下,是否是一个排列,否的话,n大于1,就把第二位的值改成第一位,n==1时,就把第一位的值改成2。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
vector<int>ar(n);
set<int>st;
bool flag=true;
for(int i=0;i<n;i++){
int xp;
cin>>xp;
if(xp>n)flag=false;
if(st.count(xp)){
flag=false;
}
else st.insert(xp);
ar[i]=xp;
}
if(!flag)cout<<0<<'\n';
else if(n!=1){
cout<<1<<'\n'<<2<<' '<<ar[0]<<'\n';
}
else cout<<1<<'\n'<<1<<' '<<2;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南