第十三届蓝桥杯 C/C++ 大学B组 题解

Posted 万伏小太阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第十三届蓝桥杯 C/C++ 大学B组 题解相关的知识,希望对你有一定的参考价值。

第十三届蓝桥杯 C/C++ 大学B组 题解

A

进制计算简单模拟

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
signed main() 
    int a[] = 2, 0, 2, 2;
    int sum = 0;
    for (int i = 0; i < 4; i++) 
        sum = sum * 9 + a[i];
    
    cout << sum << endl;
    return 0;

// 答案1478

B

遍历2022的每一天,转成字符串拼接,然后判断

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;

bool check(string s) 
    for (int i = 1; i < s.size() - 1; i++) 
        if (s[i] - s[i - 1] == 1 && s[i + 1] - s[i - 1] == 1) return true;
    
    return false;


signed main() 
    int month[] = 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31;
    int ans = 0;
    for (int i = 1; i <= 12; i++) 
        for (int j = 1; j <= month[i]; j++) 
            string y = "2022";
            if (i < 10)
                y = y + "0" + to_string(i);
            else 
                y = y + to_string(i);
            
            if (j < 10)
                y = y + "0" + to_string(j);
            else 
                y = y + to_string(j);
            
            if (check(y)) ans += 1;
            // cout << y << endl;
        
    
    cout << ans << endl;
    return 0;

C

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
signed main() 
    ll a, b, n;
    cin >> a >> b >> n;
    ll week = n / (a * 5 + b * 2);
    ll dy = n % (a * 5 + b * 2);
    ll ans = week * 7;
    if (dy <= 5 * a) 
        ans = ans + (dy + a - 1) / (a);
     else 
        ans += 5;
        dy -= 5 * a;
        ans = ans + (dy + b - 1) / b;
    
    cout << ans << endl;
    return 0;

D

找规律

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
// 暴力找出规律
vector<int> solve(int n) 
    // int n;
    // cin >> n;
    vector<int> a(n + 1, 0), ans(n + 1, 0);
    int op = 1;
    while (op <= 100) 
        for (int i = 1; i <= n; i++) 
            for (int j = 1; j <= n; j++) 
                a[j] += 1;
            
            // cout << "第" << op << "天傍晚"
            //      << "\\n";
            for (int j = 1; j <= n; j++) 
                // cout << a[j] << " ";
                ans[j] = max(ans[j], a[j]);
            
            // cout << "\\n";
            a[i] = 0;
            op += 1;
        
        for (int i = n - 1; i > 1; i--) 
            for (int j = 1; j <= n; j++) 
                a[j] += 1;
            
            // cout << "第" << op << "天傍晚"
            //      << "\\n";
            for (int j = 1; j <= n; j++) 
                // cout << a[j] << " ";
                ans[j] = max(ans[j], a[j]);
            
            a[i] = 0;
            // cout << "\\n";
            op += 1;
        
    
    // cout << n << endl;
    // for (int i = 1; i <= n; i++) 
    //     cout << ans[i] << " ";
    // 
    return ans;


void work() 
    int n;
    cin >> n;
    if (n == 1) 
        cout << 1 << endl;
        return;
    
    vector<int> vec(n + 1, 0);
    int num = (n - 1) * 2;
    for (int i = 1; i <= n / 2; i++) 
        vec[i] = vec[n - i + 1] = num;
        num -= 2;
    
    if (n % 2 == 1) 
        vec[n / 2 + 1] = n - 1;
    
    for (int i = 1; i <= n; i++) 
        cout << vec[i] << " ";
    
    cout << endl;


signed main() 
    // for (int i = 1; i <= 100; i++) 
    //     solve(i);
    //     cout << endl;
    // 
    work();
    return 0;

/*
0 0 0 第一天早上
1 1 1 第一天晚上
0 1 1 第二天早上
1 2 2 第二天晚上
1 0 2 第三天早上
2 1 3 第三天晚上
2 1 0 第四天早上
3 2 1 第四天晚上
3 0 1 第五天早上
4 1 2 第五天晚上


*/

E

主要就是看懂题意和取模的问题。

321 对应八进制、十进制、二进制

计算过程为: 3 ∗ 10 ∗ 2 + 2 ∗ 2 + 1 = 65 3*10*2+2*2+1 = 65 3102+22+1=65

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
int X, n, m;
int a[N], b[N];

int get_bit(int a, int b, int c) 
    return (a > b ? a : b) > c ? (a > b ? a : b) : c;


signed main() 
    cin >> X;
    cin >> n;
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    for (int i = n; i >= 1; i--) cin >> a[i];

    cin >> m;
    for (int i = m; i >= 1; i--) cin >> b[i];

    ll ans = 0;

    for (int i = n; i > 1; i--) 
        ans = ((ans + a[i] - b[i]) * get_bit(a[i - 1] + 1, b[i - 1] + 1, 2)) % mod;
    

    ans += a[1] - b[1];

    cout << ans << endl;
    return 0;

/*
321 65
3*10*2+2*2+1 = 65
*/

F

通过枚举上下边界,和前缀和,就转成了一维数组求子段和小于等于k的问题

经典双指针解决

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;

ll a[510][510];

signed main() 
    ll n, m, k;
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++) 
            cin >> a[i][j];
            a[i][j] += a[i - 1][j];
        
    

    ll ans = 0;

    // 通过枚举上下边界,和前缀和,就转成了一维数组求子段和小于等于k的问题 
    // 经典双指针解决

    for (int i = 1; i <= n; i++)  //枚举上边届
        for (int j = i; j <= n; j++)  // 枚举下边界
            int l = 1;
            ll sum = 0;
            for (int r = 1; r <= m; r++) 
                sum += a[j][r] - a[i - 1][r];
                while (sum > k) 
                    sum -= a[j][l] - a[i - 1][l];
                    l++;
                
                ans += r - l + 1;
            
        
    

    cout << ans << endl;

    return 0;

G

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
signed main() 
    int n;
    cin >> n;
    vector<int> dp(n + 4, 0);
    dp[1] = 1;
    dp[2] = 2;
    dp[3] = 5;
    for (int i = 4; i <= n; i++) 
        dp[i] = (dp[i - 1] * 2 % mod + dp[i - 3] % mod) % mod;
    
    cout << dp[n] << endl;

    return 0;

2022年4月第十三届蓝桥杯C/C++程序设计A组(省赛)试题及题解

目录

试题A:裁纸刀

答案为 n ∗ m − 1 + 4 n*m-1+4 nm1+4

443

试题B:灭鼠先锋

LLLV

试题C:求和

预计得分100%

思路:维护一个前缀 s u m sum sum即可。

总时间复杂度 O ( n ) O(n) O(n)

参考代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;

int a[N];

void solve()

    int n;
    scanf("%d", &n);
    long long ans = 0, sum = 0;
    for (int i = 1; i <= n; i++)
    
        scanf("%d", &a[i]);
        ans += sum * a[i];
        sum += a[i];
    
    cout << ans << endl;

int main()

    solve();
    return 0;

试题 D: 选数异或

预计得分100%

对于每个位置 i i i ,设 y = a [ i ] y = a[i] y=a[i] ^ x x x,找到最近的一个 y y y的下标 i d x idx idx,记作 b [ i ] b[i] b[i]。再用线段树维护 b b b数组的区间最大值 m a x x maxx maxx,如果 m a x x > = L maxx >= L maxx>=L,那么为 y e s yes yes

总时间复杂度 O ( n l o g n ) O(nlogn) O(nlogn)

参考代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 3e5 + 5;

int a[N], b[N];

struct node

    int l, r, val, maxx;
 tr[N * 4];

void pushup(int k)  tr[k].maxx = max(tr[k * 2].maxx, tr[k * 2 + 1].maxx); 
void build(int k, int l, int r)

    tr[k].l = l;
    tr[k].r = r;
    if (tr[k].l == tr[k].r)
    
        tr[k].val = tr[k].maxx = b[l];
        return;
    
    int mid = l + r >> 1;
    build(k * 2, l, mid);
    build(k * 2 + 1, mid + 1, r);
    pushup(k);


int query(int k, int l, int r)

    if (tr[k].l == l && tr[k].r == r)
        return tr[k].maxx;
    int mid = tr[k].l + tr[k].r >> 1;
    if (r <= mid)
        return query(k * 2, l, r);
    else if (l > mid)
        return query(k * 2 + 1, l, r);
    else
        return max(query(k * 2, l, mid), query(k * 2 + 1, mid + 1, r));


void solve()

    int n, m, x;
    scanf("%d %d %d", &n, &m, &x);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    map<int, int> last;
    for (int i = 1; i <= n; i++)
    
        int y = a[i] ^ x;
        if (!last.count(y))
            b[i] = -1;
        else
            b[i] = last[y];
        last[a[i]] = i;
    
    build(1, 1, n);
    while (m--)
    
        int l, r;
        scanf("%d %d", &l, &r);
        int maxx_pos = query(1, l, r);
        puts(maxx_pos >= l ? "yes" : "no");
    


int main()

    solve();
    return 0;

试题 E: 爬树的甲壳虫

暂时不会。

试题 F: 青蛙过河

2022.4.25 UPDATE:少了一个语句,已修正

预计得分100%

二分经典题改编题,一眼二分,check有点难写。

check思路:

可以令 d p [ i ] dp[i] dp[i]表示跳到第 i i i个石头的最大次数。
把最后 m i d mid mid个石头的 d p [ i ] dp[i] dp[i]加起来, s u m > = 2 ∗ x sum>= 2*x sum>=2x即合法。

总时间复杂度 O ( n l o g n ) O(nlogn) O(nlogn)

参考代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 3e5 + 5;
int a[N];

long long dp[N];
// dp[i]表示最多能跳到位置i dp[i]次

int n, x;
bool check(int mid)

    long long sum = 0;
    for (int i = 1; i <= mid; i++) //前mid个都可以
        dp[i] = a[i];
    int l = 1;
    for (int i = mid + 1; i <= n; i++)
    
        while (i - l > mid)//控制跳跃距离。
            ++l;
        dp[i] = 0;
        while (dp[i] < a[i] && l < i)
        
            if (dp[l] + dp[i] <= a[i])
            
                dp[i] += dp[l];
                dp[l] = 0;
                ++l;
            
            else
            
                dp[l] -= a[i] - dp[i];
                dp[i] = a[i];
            
        
    
    long long ans = 0;
    int L = n - mid + 1;
    for (int i = L; i <= n; i++)
        ans += dp[i];
    return ans >= 2 * x;


void solve()

    scanf("%d %d", &n, &x);
    --n;
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    int l = 1, r = n, ans = n + 1;
    while (l <= r)
    
        int mid = l + r >> 1;
        if (check(mid))
        
            ans = mid;
            r = mid - 1;
        
        else
            l = mid + 1;
    
    printf("%d\\n", ans);


int main()

    solve();
    return 0;


试题 G: 最长不下降子序列

预计得分10% ~ 30%

暴力思路:

枚举修改位置,二分法 O ( n l o g n ) O(nlogn) O(nlogn)求最长不下降子序列,(跳过中间那段长度为 k k k的数组)

总时间复杂度 O ( n 2 l o g n ) O(n^2logn) O(n2logn)

参考代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;

int a[N], dp[N];
int n, k;
int LIS(int L, int R) //二分求LIS

    int len = 0;
    dp[len] = -0x3f3f3f3f;
    for (int i = 1; i <= n; i++)
    
        if (i == L) //跳过k个
        
            i = R;
            continue;
        

        if (a[i] >= dp[len])
        
            ++len;
            dp[len] = a[i];
        
        int p = upper_bound(dp + 1, dp + 1 + len, a[i]) - dp;
        dp[p] = a[i];
    
    return len;

void solve()

    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    if (n - 1 <= k)
    //修改k个必然能全部满足
    
        printf("%d\\n", n);
        return;
    
    int ans = k + 1;
    for (int i = 1; i + k - 1 <= n; i++)
        ans = max(ans, k + LIS(i, i + k - 1));
    printf("%d\\n", ans);


int main()

    solve();
    return 0;


试题 H: 扫描游戏

预计得分10% ~ 30%

暴力思路:

极角排序。之后每次暴力扫描,循环直到扫描不到物品。

总时间复杂度 O ( n 2 ) O(n^2) O(n2)

参考代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 3e5 + 5;
const int INF = 0x3f3f3f3f;
struct node

    long long x, y, z, id;
    double theta;
    bool operator<(const node &tmp) const  return theta < tmp.theta; 
;

vector<node> vec;
int out[N];
node tmp;
void solve()

    int n;
    long long L;
    scanf("%d %lld", &n, &L);
    for (int i = 1; i <= n; i++)
    
        out[i] = -1;
        tmp.id = i;
        scanf("%lld %lld %lld", &tmp.x, &tmp.y, 2022 第十三届蓝桥杯大赛软件赛省赛(第二场),C/C++ 大学B组题解

第十三届蓝桥杯大赛软件赛省赛(C/C++ 大学B组)

第十三届蓝桥杯大赛软件赛省赛 C/C++ 大学 B 组思考+总结

2022年4月第十三届蓝桥杯C/C++程序设计A组(省赛)试题及题解

2022年4月第十三届蓝桥杯C/C++程序设计A组(省赛)试题及题解

2022年4月第十三届蓝桥杯C/C++程序设计A组(省赛)试题及题解