比赛
链接:Dashboard - Codeforces Round 719 (Div. 3) - Codeforces
时间:2024.3.3
一、第一题过了
二、Do Not Be Distracted!
题目描述
十进制中每个数位都相同,则称这个数为普通数。求1~n中有几个普通数。
思路
枚举所有普通数(因为普通数在1e9范围内只有81个),找出n,然后枚举过程中计数。
AC代码
#include<bits/stdc++.h>
#define TIE ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int t,n,c[26];string s;
signed main(){
TIE;cin>>t;
while(t--){
bool f=0;memset(c,0,sizeof c);cin>>n>>s;
for(int i=0;i<n;i++){
if(c[s[i]-'A']&&s[i]!=s[i-1]){f=1;break;}
if(!i||s[i]!=s[i-1])c[s[i]-'A']++;
}
if(f)cout<<"NO\n";
else cout<<"YES\n";
}
return 0;
}
三、Same Differences
题目描述
如果一个正方形矩阵中两个格子有公共边,则两个格子是相邻的。
构造一个n*n的矩阵,1~n^2在矩阵中都值出现一次,且相邻的格子数不相邻。
思路
如果n为1,直接输出1即可。
否则,先将全部奇数放入格子,再将全部偶数放入格子,奇数间与偶数间一定不相邻。
需要特殊考虑n=2的情况,此时是无法画出符合要求的矩阵的。
AC代码
#include<bits/stdc++.h>
#define TIE ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int t,n;
signed main(){
TIE;cin>>t;
while(t--){
cin>>n;
if(n==1)cout<<1<<'\n';
else if(n==2)cout<<-1<<'\n';
else{
int m=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<m<<' ';m+=2;
if(m>n*n)m=2;
}
cout<<'\n';
}
}
}
return 0;
}
四、Same Differences
题目描述
输入n个数,找出有几对数值差等于下标差的数。
思路
数值差等于下标差:
变换以下可得:
所以可以创建一个数组,用桶记录下每个出现的次数。最后输出总和。
AC代码
#include<bits/stdc++.h>
#define TIE ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define ll long long
using namespace std;
const int N=2e5+5;
int t,n,a[N],b[N];
signed main(){
TIE;cin>>t;
while(t--){
cin>>n;ll cnt=0;memset(b,0,sizeof b);
for(int i=1;i<=n;i++){cin>>a[i];b[a[i]-i]++;}
for(int i=0;i<N;i++)cnt+=b[i];cout<<cnt<<'\n';
}
return 0;
}