【蓝桥杯专题】枚举、模拟与排序 (C++ | 洛谷 | acwing | 蓝桥)

菜狗现在才开始备战蓝桥杯QAQ


在这里插入图片描述

【蓝桥杯专题】 (C++ | 洛谷 | acwing | 蓝桥)

回文日期

在这里插入图片描述

链接 链接

#include <bits/stdc++.h>
// #include <iostream>
using namespace std;
typedef long long ll;
typedef double db;
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define per(i, a, n) for(int i = n; i <= a; i --)
#define pb push_back;
#define fs first;
#define sz second;
#include <stdlib.h> // atoi
#define debug cout<<"debug"<<"\n"
#define endl "\n";
const int INF = 0x3f3f3f3f;
const int mod=1e9+7;
const int N = 1e5 + 10;

int months[13] = {
    
    0,31,28,31,30,31,30,31,31,30,31,30,31};

// int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check (int date) {
    
    
    int year = date / 10000;
    int month = date % 10000 / 100;
    int day = date % 100;
    
    //判断 离谱日期
    if(month > 13 || !day || !month) return false;
    
    
    // 判断月份对应日期
    if(month != 2 && day > months[month]) return false;
    
    
    //判断润年
    if(month == 2) {
    
    
        //如果能被 400 整除  并且 能被四整除 或不能被100整除
        bool leap = year % 400 == 0 &&  year % 4 == 0 || year % 100;
        if(day > leap + 28) return false;
    }
    
    return true;
}

void solve () {
    
    
    int s1, s2;
    cin >> s1 >> s2;
    int res = 0;    
    for(int i = 1; i < 10000; i ++) {
    
    
        int x = i, r = i;
        for(int j = 0; j < 4; j ++) {
    
    
            r = r * 10 + x % 10, x /= 10;
        }
        if(r >= s1 &&  r <= s2 && check(r)) res ++;
    }

    cout << res << endl;
}

int main(void){
    
    
// 	freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int T = 1;
    // cin >> T;
	while(T --) solve();
	return 0;
}

纸张尺寸 蓝桥杯真题

  • 模拟题
    scanf 新操作
    在这里插入图片描述

错误票据

链接 链接

  • 读入 可以学一下
#include <bits/stdc++.h>
// #include <iostream>
using namespace std;
typedef long long ll;
typedef double db;
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define per(i, a, n) for(int i = n; i <= a; i --)
#define pb push_back;
#define fs first;
#define sz second;
#include <stdlib.h> // atoi
#define debug cout<<"debug"<<"\n"
#define endl "\n";
const int INF = 0x3f3f3f3f;
const int mod=1e9+7;
const int N = 1e5 + 10;

int a[N];
void solve () {
    
    
    string line;
    int n = 0;
    int cnt;
    cin >> cnt;
    getline(cin,line);
    while (cnt --) {
    
     // 空间多开点  因为很大 80 一行可能10个 就是 800 甚至更多
        getline(cin,line);
        stringstream ssin(line);
        while(ssin >> a[n]) n ++;
    }
    sort(a,a + n + 1);
    int res1, res2;
    for(int i = 0; i <= n; i ++) {
    
    
        if(a[i] == a[i - 1] + 2)res1 = a[i - 1] + 1;
        if(a[i] == a[i - 1]) res2 = a[i];
    }
    cout << res1 << ' ' << res2 << endl;
}

int main(void){
    
    
	freopen("in.txt","r",stdin);     
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int T = 1;
    // cin >> T;
	while(T --) solve();
	return 0;
}

AcWing 788. 逆序对的数量

链接 链接

//归并排序


#include <bits/stdc++.h>
// #include <iostream>
using namespace std;
typedef long long ll;
typedef double db;
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define per(i, a, n) for(int i = n; i <= a; i --)
#define pb push_back;
#define fs first;
#define sz second;
#include <stdlib.h> // atoi
#define debug cout<<"debug"<<"\n"
#define endl "\n";
const int INF = 0x3f3f3f3f;
const int mod=1e9+7;
const int N = 1e5 + 10;

ll res = 0;
int q[N], w[N];

void merge_sort(int q[], int l, int r) {
    
        
    if(l >= r) return;
    int mid = l + r >> 1;
    merge_sort(q, l, mid) ,merge_sort(q, mid + 1, r);

    int i = l, j = mid + 1, k = 0;
    while(i <= mid && j <= r) {
    
    
        if(q[i] <= q[j]) w[k ++] = q[i ++];
        else {
    
    
            w[k ++] = q[j ++];
            res += mid - i + 1; // 与递归的区别 , 把不同元素区间
        }
    } 
    while(i <= mid ) w[k ++] = q[i ++];
    while(j <= r) w[k ++] = q[j ++];

    for(i = l, j = 0; i <= r; i ++, j ++) q[i] = w[j];

}

void solve () {
    
    
    int n;
    cin >> n;
    rep(i, 0, n - 1) {
    
    
        cin >> q[i];
    }

    merge_sort(q, 0, n - 1);


    cout << res <<endl;
}

int main(void){
    
    
// 	freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int T = 1;
    // cin >> T;
	while(T --) solve();
	return 0;
}

航班时间

 scanf("%d(+%d)%d", &a, &b, &c);   如果不匹配 直接忽略 b的读

链接 链接

#include<iostream>
#include<cstring>
#include<cstdio>
#include <limits.h>
#include<algorithm>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef double db;
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define per(i, a, n) for(int i = n; i <= a; i --)
#define pb push_back;
#define fs first;
#define sz second;
#include <stdlib.h> // atoi
#define debug cout<<"debug"<<"\n"
#define endl "\n";
const int INF = 0x3f3f3f3f;
const int mod=1e9+7;
const int N = 1e5 + 10;

int getTime(void)
{
    
    
    int h1,m1,s1,h2,m2,s2,d=0;
    scanf("%d:%d:%d %d:%d:%d (+%d)",&h1,&m1,&s1,&h2,&m2,&s2,&d);
    int time=d*24*3600+h2*3600+m2*60+s2-(h1*3600+m1*60+s1);
    return time;
}

void solve () {
    
    
    int t;
    scanf("%d",&t);
    for(int i = 0; i < t; i++)
    {
    
    
        int time1=getTime();
        int time2=getTime();
        int t=(time1+time2)/2;
        printf("%02d:%02d:%02d\n", t/3600, t/60%60, t%60);
    }
}

int main(void){
    
    
	freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int T = 1;
    cin >> T;
	while(T --) solve();
	return 0;
}

移动距离

链接 链接

  • 简单数学模拟 + 类似蛇形填数
#include<iostream>
#include<cstring>
#include<cstdio>
#include <limits.h>
#include<algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef double db;
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define per(i, a, n) for(int i = n; i <= a; i --)
#define pb push_back;
#define fs first;
#define sz second;
#include <stdlib.h> // atoi
#define debug cout<<"debug"<<"\n"
#define endl "\n";
const int INF = 0x3f3f3f3f;
const int mod=1e9+7;
const int N = 1e5 + 10;

void solve () {
    
    
	int w, n, m;
    cin >> w >> n >> m;
    m -- , n --;
    int x1 = m % w;
    int y1 = m / w;
    int x2 = n % w;
    int y2 = n / w;
    if(y1 % 2) x1 = w - x1 - 1;
    if(y2 % 2) x2 = w - x2 - 1;
    int ans = abs(x2 - x1) + abs(y2 - y1);
    cout << ans << endl;
}

int main(void){
    
    
	freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int T = 1;
    // cin >> T;
	while(T --) solve();
	return 0;
}

链接 链接



连号区间

  • 思路 : 区间连续 则 边界差 == max - min
    在这里插入图片描述

链接 链接

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int a[10010];

int main () {
    
    
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++) {
    
    
        cin >> a[i];
    }
    long long res = 0;
    
    
    //因为是连续的所以在所取的[l,r]范围中寻找最大值,
    // 最小值。然后相减,最后和r-l(区间长度)作比较即可。 
    // 范围 i ~ j
    for(int i = 0; i < n; i ++) {
    
    
            int minn = 10000;
            int maxx = -10000;
        for(int j = i; j < n; j ++ ) {
    
    
             maxx = max(maxx, a[j]); // 更新区间值
             minn = min(minn, a[j]);
            if((j - i)  == (maxx - minn)) res ++;
        }
    }
    cout <<res <<endl;
    
    return 0;
}

1236. 递增三元组

枚举一个数 b 二分查找 a,c
用二分模板 O(n * logn) 找完需要判断一下找到的值与 b[i]比较
查找A:由于枚举的是B,所以查找A就只需要它最右边的那个数,也就是小于b[i]的最大值,使用SL模板
查找C:同上,找的是大于b[i]的最小值 ,也就是最左边,用SR即可。
链接 链接


#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 100010;
int a[N], b[N], c[N];


int main () {
    
    
    int n;
    
    long long res = 0;
    cin >> n;
    for (int i = 0; i < n; i ++ ) cin >> a[i];
    
    for (int i = 0; i < n; i ++ ) cin >> b[i];
    
    for (int i = 0; i < n; i ++ ) cin >> c[i];
    
    sort(a, a + n);
    sort(b, b + n);
    sort(c, c + n);
    
    for(int i = 0; i < n; i ++) {
    
    
        int x = b[i];
        int l = 0, r = n - 1;
        int aa = 0, bb = 0;
        while(l < r) {
    
    
            int mid = l + r + 1 >> 1;
            if(a[mid] < x) l = mid;
            else r = mid - 1;
        }
        
        //二分结果未必找得到  所以需要判断
        if(a[l] >= b[i]) {
    
    
            r = -1;
        } 
            aa = r;
        
        l = 0, r = n - 1;
        while(l < r) {
    
    
            int mid = (l + r) / 2;
            if(c[mid] > x) r = mid;
            else l = mid + 1;
        }
        
        //二分结果未必找得到  所以需要判断
        if(b[i] >= c[l]){
    
    
            r = n;
        } 
        bb = r;
    
        
        
        res += (long long )(aa + 1) * (n - bb);
    }
    cout << res <<endl;
    
    return 0;
}

P

链接 链接



P

链接 链接



P

链接 链接



P

链接 链接



P

链接 链接



猜你喜欢

转载自blog.csdn.net/weixin_49486457/article/details/129416578
今日推荐