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

题目: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;
}

题目:466. 回文日期

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

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int date1,date2;
int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int u){
    if(u<date1||u>date2) return false;
    int y=u/10000;
    int m=u%10000/100;
    int d=u%100;
    if(m<=0||m>12|| d<=0) return false;
    if(m!=2 &&d>mon[m]) return false;
    if(m==2){
        int t=0;
        if(y%400==0||y%4==0&&y%100!=0) t=1;
        if(d>mon[m]+t) return false;
    }
    return true;
}
int main(){
    cin>>date1>>date2;
    int s=date1/10000;
    int e=date2/10000;
    int ans=0;
    for(int i=s;i<=e;i++){
        int t=i;
        t=t*10000+t%10*1000+t%100/10*100+t%1000/100*10+t/1000;
        //cout<<endl<<t;
        if(check(t)) ans++;
    }
    cout<<ans;
    return 0;
}

题目:787. 归并排序

在这里插入图片描述
题解:归并排序模板题

#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];
void merge_sort(int l,int r){
    if(l>=r) return ;
    int mid=l+r>>1;
    merge_sort(l,mid);
    merge_sort(mid+1,r);
    int t[r-l+5];
    int k=0;
    int i=l,j=mid+1;
    while(i<=mid&&j<=r){
        if(a[i]<=a[j])t[k++]=a[i++];
        else t[k++]=a[j++];
    }
    while(i<=mid) t[k++]=a[i++];
    while(j<=r) t[k++]=a[j++];
    for(int i=0;i<k;i++)
        a[l+i]=t[i];
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    merge_sort(1,n);
    for(int i=1;i<=n;i++){
        printf("%d ",a[i]);
    }
    return 0;
}

题目:1219. 移动距离

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

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int m,n,w;
int main(){
    cin>>w>>m>>n;
    m--,n--;
    int r1=m/w;
    int r2=n/w;
    int c1=m%w;
    int c2=n%w;
    if(r1%2){
        c1=w-c1-1;
    }
    if(r2%2){
        c2=w-c2-1;
    }
    cout<<abs(r1-r2)+abs(c1-c2);
    return 0;
}

题目:1229. 日期问题

在这里插入图片描述
题解:模拟+枚举。可能的日期范围内时间复杂度是10^6。所以模拟更便捷

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int u){
    int y=u/10000;
    int m=u%10000/100;
    int d=u%100;
    if(m<=0||m>12|| d<=0) return false;
    if(m!=2 &&d>mon[m]) return false;
    if(m==2){
        int t=0;
        if(y%400==0||y%4==0&&y%100!=0) t=1;
        if(d>mon[m]+t) return false;
    }
    return true;
}
int main(){
    int a,b,c;
    scanf("%d/%d/%d",&a,&b,&c);
    for(int i=19600101;i<=20591231;i++){
        if(check(i)){
            int d=i%100;
            int m=i%10000/100;
            int y=i%1000000/10000;
            if(y==a&&m==b&&d==c||a==d&&b==m&&c==y||a==m&&b==d&&c==y){
                printf("%d-%02d-%02d\n",i/10000,m,d);
            }
        }
    }
    return 0;
}

题目:1231. 航班时间

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

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int t;
int main(){
    cin>>t;
    //getchar();
    while(t--){
        int a,b,c,d,e,f,m=0;
        scanf("%d:%d:%d %d:%d:%d (+%d)",&a,&b,&c,&d,&e,&f,&m);
        int t1=d*60*60+e*60+f+m*24*60*60-a*60*60-b*60-c;
        m=0;
        scanf("%d:%d:%d %d:%d:%d (+%d)",&a,&b,&c,&d,&e,&f,&m);
        int t2=d*60*60+e*60+f+m*24*60*60-a*60*60-b*60-c;
        //cout<<endl<<t1<<" "<<t2<<endl;
        int t3=(t1+t2)/2;
        printf("%02d:%02d:%02d\n",t3/3600,t3/60%60,t3%60);
    }
    return 0;
}

题目:1241. 外卖店优先级

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

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=1e5+10;
const int mod=100000007;
int n,m,t;
bool sta[N];
int a[N];
int b[N];
PII ts[N];
int main(){
    cin>>n>>m>>t;
    int x,y;
    for(int i=1;i<=m;i++){
        scanf("%d %d",&ts[i].first,&ts[i].second);
    }
    sort(ts+1,ts+1+m);
    for(int i=1;i<=m;i++){
        x=ts[i].first,y=ts[i].second;
        if(a[y]<x){
            b[y]=max(0,b[y]-(x-a[y]-1));
        }
        if(sta[y]&&b[y]<=3) sta[y]=0;//一定要在下面+2之前进行判断
        b[y]+=2;
        if(b[y]>5) sta[y]=1;
        
        a[y]=x;
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        if(sta[i]){
            b[i]=max(0,b[i]-(t-a[i]));
            if(b[i]<=3) sta[i]=0;
        }
        if(sta[i]){
            ans++;
            //cout<<i<<endl;
        }
    }
    cout<<ans;
    return 0;
}

题目:788. 逆序对的数量

在这里插入图片描述
题解:归并排序

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=1e5+10;
const int mod=100000007;
int n;
int a[N];
LL ans=0;
void merge_sort(int l,int r){
    if(l>=r) return;
    int mid=l+r>>1;
    merge_sort(l,mid);
    merge_sort(mid+1,r);
    int t[r-l+5];
    int k=0;
    int i=l,j=mid+1;
    while(i<=mid&&j<=r){
        if(a[i]<=a[j]){
            t[k++]=a[i++];
        }else{
            ans+=mid-i+1;
            t[k++]=a[j++];
        }
    }
    while(i<=mid){
        //ans+=r-mid;
        t[k++]=a[i++];
    }
    while(j<=r){
        t[k++]=a[j++];
    }
    for(int i=0;i<k;i++){
        a[l+i]=t[i];
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    merge_sort(1,n);
    printf("%lld",ans);
    return 0;
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
蓝桥杯 Java B 组真题解析 12 这道蓝桥杯 Java B 组的真题是一道暴力枚举目,大致的意思就是求解给定的若干个数之中是否存在某两个数的和能够被给定的一个数整除。 这个问其实可以通过枚举法来解决,对于给定的若干个数中的任意两个数进行求和,并检查其能否被给定的那个数整除。如果存在这样的两个数,则输出 Yes,否则输出 No。 枚举法虽然思路简单,但是其时间复杂度很高。因此,对于这道目,我们需要使用一些较为高效的算法来进行优化。下面是一些可能有用的技巧和优化: 1. 对于求和能够被整除的问,我们可以使用模运算符 (%) 来进行优化。如果 a + b 能够被 n 整除,那么 a % n 和 b % n 的和一定也能够被 n 整除。 2. 对于较大的数据集,我们可以使用哈希表来进行优化。我们可以将每个数都存储在哈希表中,然后对于每一对数进行求和并检查是否存在一个数在哈希表中。如果存在,则说明这一对数之和能够被 n 整除。 3. 对于数据规模较小的情况,我们可以使用双重循环来进行暴力枚举。双重循环的时间复杂度为 O(n^2),但是其编码简单,对于小数据集来说效率也相对较高。 4. 如果所有的数都不能被 n 整除,那么其和也一定不能被 n 整除。因此,我们可以考虑对所有的数进行求和并进行模运算,以此来减少不必要的计算。 总之,这道蓝桥杯 Java B 组真题没有特别难的地方,但是其考察了枚举法的思想和编程实现能力。希望所有参加蓝桥杯竞赛的选手都能够顺利通过此,取得好成绩。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值