Codeforces Round 923 (Div. 3)

文章描述了使用C++编程解决涉及数组操作、区间查询、模拟以及数据结构(如双指针和集合)的问题,包括找到特定字符子串的区间、判断满足特定条件的元素组合等。

A.

找第一个和最后一个

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+9;
int a[N];
void lan(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    int l=0;
    int r=0;
    bool flag=true;
    for(int i=0;i<n;i++){
        if(s[i]=='B' && flag){
            l=i;
            r=i;
            flag=false;
        }else if(s[i]=='B' && !flag){
            r=i;
        }
    }
    cout<<r-l+1<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int q;
    cin>>q;
    while(q--){
        lan();
    }
    return 0;
}

B.

模拟

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+9;
int a[N],c[N];
void lan(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    char t='a';
    for(int i=1;i<=n;i++){
        if(!a[i]){
            cout<<t;
            c[t-'a']++;
            t=char(t+1);
        }else{
            for(int j=0;j<=27;j++){
                if(c[j]==a[i]){
                    cout<<char('a'+j);
                    c[j]++;
                    break;
                }
            }
        }
    }
    for(int i=0;i<=27;i++){
        c[i]=0;
    }
    cout<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int q;
    cin>>q;
    while(q--){
        lan();
    }
    return 0;
}

C.

怎么才能满足题意

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+9;
int a[N],b[N];
void lan(){
    int n,m,k;
    cin>>n>>m>>k;
    set<ll> sta,stb,stt;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(a[i]<=k){
            sta.insert(a[i]);
            stt.insert(a[i]);
        }
    }
    for(int i=1;i<=m;i++){
        cin>>b[i];
        if(b[i]<=k){
            stb.insert(b[i]);
            stt.insert(b[i]);
        }
    }
    //分析1.a独有少于k/2,不符合题意
    //分析2.b同理,
    //分析3.a+b必然要满足k才可以
    if(sta.size()>=k/2 && stb.size()>=k/2 && stt.size()==k){//a能拿k/2,b能拿k/2,总和能拿k个
        cout<<"YES"<<'\n';
    }else{
        cout<<"NO"<<'\n';
    }

}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int q;
    cin>>q;
    while(q--){
        lan();
    }
    return 0;
}

D.

双指针思想预处理,还有st表,线段树,dp...做法

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+9;
int a[N],cl[N];
void lan(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=2;i<=n;i++){//记录左端点
        cl[i]=a[i]==a[i-1]?cl[i-1]:i-1;
    }
    //记录右端点
    // cl[n]=n;
    // for(int i=n-1;i>=1;i--){
    //     cl[i]=a[i]==a[i+1]?cl[i+1]:i;
    // }
    int q;
    cin>>q;
    while(q--){
        int l,r;
        cin>>l>>r;
        if(cl[r]>=l){//if(cl[l]<r)
            cout<<cl[r]<<" "<<r<<'\n';//cout<<l<<" "<<cl[l]+1<<'\n';
        }else{
            cout<<-1<<" "<<-1<<'\n';
        }
    }
    cout<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int q;
    cin>>q;
    while(q--){
        lan();
    }
    return 0;
}

E.

构造

如何构造连续k任意相差1,只能往返运动()

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+9;
int a[N];
void lan(){
    int n,k;
    cin>>n>>k;
    //加减加减,每一个连续k序列,sum,sum-1,sum,sum-1...
    //正的来一遍反正来一遍
    //k=4,n=12,(12,11,10)(1,2,3)(9,8,7)(4,5,6);
    //12,1,9,4,11,2,8,5,10,3,7,6;
    //如果不整除少几个如1,6,7,12,2,5,8,11,3,4
    int l=1,r=n;//小,大
    for(int i=1;i<=k;i++){
        //先构造每个k位置再k+1...
        for(int j=i;j<=n;j+=k){
            if(i&1){//奇数给大
                a[j]=r--;
            }else{//偶数给小
                a[j]=l++;
            }
        }
    }
    for(int i=1;i<=n;i++){
        cout<<a[i]<<" ";
    }
    cout<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int q;
    cin>>q;
    while(q--){
        lan();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值