@TOC
A - Do Not Be Distracted!
-
题意:判断是否在后面(不相邻)出现同一字符
-
思路:先去重(去掉相邻的重复的),再判断(判断去重串是否有前后重复的)
答案
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
const double eps = 1e-6;
const int mod = 998244353;
const int N = 2e5 + 10;
const int M = 5555;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;
void solve(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
//getchar();
string s;
cin>>s;
// cout<<s<<endl;
string s1="";
s1+=s[0];
for(int i=1;i<n;i++){
if(s[i]!=s[i-1]) s1+=s[i];
}
bool flag=0;
int len=s1.size();
for(int i=0;i<len;i++){
for(int j=i+1;j<len;j++){
if(s1[i]==s1[j]){
flag=1;
break;
}
}
}
if(!flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
B - Ordinary Numbers
- 题意:给定一个n,判断1~n范围内有多少个普通数(普通数定义:只有一个数字组成 e.g. 1、111、1111……)
- 思路:很容易发现每一个进位当中就会有9个普通数(1~9:9个 10 ~99:9个……)。 所以接下来:
- 先去找到这个数字有多少位(pos=位数)(pos-1)*9是pos-1位共有的普通数的个数
- 找到n的第一位数字nn,易知 有pos位时1到nn-1组成的普通数都满足条件
- 判断nn组成的数字和n比较,若n>nn组成的数字则ans++
答案
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
const double eps = 1e-6;
const int mod = 998244353;
const int N = 2e5 + 10;
const int M = 5555;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;
void solve(){
int t;
cin>>t;
while(t--){
ll n;
cin>>n;
int pos=0;
ll nn=n;
while(nn){
if(nn/10) {
pos++;
nn/=10;
}
else break;
}
ll ans=pos*9;
ans+=(nn-1);
ll sum=0;
for(int i=1;i<=pos+1;i++){
sum=sum*10+nn;
}
if(sum<=n) ans++;
cout<<ans<<endl;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
C - Not Adjacent Matrix
- 题意:简单模拟。要求我们填数字,使得该数字与其上下左右四个方向每个数字的差不得小于2,即|a-b|>1。
- 思路:先填奇数,后填偶数,填完即可。
答案
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
const double eps = 1e-6;
const int mod = 998244353;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;
int mp[M][M];
void solve(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
if(n==1) cout<<"1"<<endl;
else if(n==2) cout<<"-1"<<endl;
else{
int tot=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(tot>n*n) tot=2;
mp[i][j]=tot;
tot+=2;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(j==n) cout<<mp[i][j]<<endl;
else cout<<mp[i][j]<<" ";
}
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
D - Same Differences
- 题意:给定一个序列,问当 i<j 时满足
aj - ai = j - i
的数量 - 思路:用值减去其下标,相等的即可以满足条件,然后算出每个组合的个数,针对每个个数求一个
1+2+…+n
的加和,将和加到最后答案上
答案
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
const double eps = 1e-6;
const int mod = 998244353;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;
//int dp[N];
map<int,int>mp;
void solve(){
int t;
cin>>t;
while(t--){
mp.clear();
int n;
cin>>n;
// int tot=0;
for(int i=1;i<=n;i++){
int k;
cin>>k;
k-=i;
mp[k]++;
}
ll ans=0;
map<int,int>::iterator i;
for(i=mp.begin();i!=mp.end();i++){
ll sum=(*i).second;
ans+=(sum)*(sum-1)/2;
}
cout<<ans<<endl;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
E - Arranging The Sheep
- 题意:给定一个字符串只有
*
和.
,*
是羊,.
是空地,羊只能在当前位置的左右移动,问多少次操作可以把所有的羊靠在一起 - 思路:找到中间的羊的位置,所有的羊往中间靠拢,详见代码实现
答案
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
const double eps = 1e-6;
const int mod = 998244353;
const int N = 1e6 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;
//char dp[N];
int shep[N];
//map<int,int>mp;
void solve(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
// getchar();
string s;
cin>>s;
// cout<<s<<endl;
int tot=0;
for(int i=0;i<n;i++){
// cin>>dp[i];
if(s[i]=='*') shep[++tot]=i+1;
}
int pos=shep[(tot+1)/2];
ll ans=0;
for(int i=1;i<=tot;i++){
ans+=abs(shep[i]-pos)-abs(i-(tot+1)/2);
}
cout<<ans<<endl;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}