0
点赞
收藏
分享

微信扫一扫

蓝桥杯专题训练之枚举、模拟与排序(相关真题和模板题)

前端王祖蓝 2022-03-30 阅读 74

题目:1210. 连号区间数

在这里插入图片描述
题解:枚举。复杂度最多为0(10^7)

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int n;
int a[10010];
int main(){
    cin>>n;
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    LL ans=0;
    for(int i=0;i<n;i++){
        int maxx=-100,minn=10010;
        for(int j=i;j<n;j++){
            maxx=max(maxx,a[j]);
            minn=min(minn,a[j]);
            if(maxx-minn==j-i)
                ans++;
        }
    }
    printf("%lld",ans);
    return 0;
}

题目:1236. 递增三元组

在这里插入图片描述
题解:枚举+二分

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int n;
int a[N],b[N],c[N];
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&c[i]);
    }
    sort(a+1,a+n+1);
    sort(b+1,b+1+n);
    sort(c+1,c+1+n);
    LL ans=0;
    for(int i=1;i<=n;i++){
        int ct_a=lower_bound(a+1,a+1+n,b[i])-a-1;
        int ct_c=n-(upper_bound(c+1,c+1+n,b[i])-c)+1;
        ans+=1ll*ct_a*ct_c;//注意10^5*10^5会超过int
        }
    printf("%lld",ans);
    return 0;
}

题目:1245. 特别数的和

在这里插入图片描述
题解:模拟

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int n;
int main(){
    cin>>n;
    LL ans=0;
    for(int i=1;i<=n;i++){
        int t=i;
        while(t){
            int x=t%10;
            t/=10;
            if(x==2||x==0||x==1||x==9){
                ans+=i;
                break;
            }
        }
    }
    printf("%lld",ans);
    return 0;
}

题目:1204. 错误票据

在这里插入图片描述
题解:排序+模拟

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int n;
int a[10010];
int k=0;
int main(){
    cin>>n;
    getchar();
    string s;
    while(n--){
        getline(cin,s);
        stringstream ssin(s);
        while(ssin>>a[k])k++;//这里的k++,不能放在a[k++];
    }
    sort(a,a+k);
    int l=0,r=0;
    for(int i=1;i<k;i++){
        if(a[i]==a[i-1]) r=a[i];
        else if(a[i]>=a[i-1]+2) l=a[i]-1;
        
    }
    cout<<l<<" "<<r;
    return 0;
}

举报

相关推荐

0 条评论