Codeforces Round #719 (Div. 3) 题解(详解)

@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个……)。 所以接下来:
  1. 先去找到这个数字有多少位(pos=位数)(pos-1)*9是pos-1位共有的普通数的个数
  2. 找到n的第一位数字nn,易知 有pos位时1到nn-1组成的普通数都满足条件
  3. 判断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;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值