2025蓝桥杯C++国A题解

暴力杯打爆炸了,啥也不会,狠狠补题吧。



C. 土地整平计划

题意

给出一个数组 \(a\) 。如果 \(a_i\) 左边所有数都和它不相等,那么可以把左边所有数都改成 \(a_i\) ,花费为 \(a_i\) ;对右边同理。如果有 \(a_i = a_j\) 且它们之间的所有数都和它不相等,那么可以把中间所有数改成 \(a_i\) ,花费为 \(a_i\) 。每个位置最多被修改一次。求把整个数组变成同一个数的最小花费。

题解

既然每个位置最多被修改一次,那修改方案就只有一种:对于某个已存在的数,看整个数组有几个连续区间不等于它,它对应的答案就是自身乘上区间数。

#include <bits/stdc++.h>
#define int long long
constexpr int N = 1e6 + 5;
constexpr int INF = 2e16;

int n, h[N], cnt[N];
bool f[N];

void solve() {
    std::cin >> n;
    int x, tot = 0;
    for (int i = 1; i <= n; i++) {
        std::cin >> x;
        if (x != h[tot]) h[++tot] = x;
    }
    if (tot == 1) {
        std::cout << "0\n";
        return;
    }
    for (int i = 1; i <= tot; i++) cnt[h[i]]++, f[h[i]] = 1;
    cnt[h[1]]--, cnt[h[tot]]--;
    int ans = INF;
    for (int i = 1; i <= (int)1e6; i++) {
        if (!f[i]) continue;
        ans = std::min(ans, i * (cnt[i] + 1));
    }
    std::cout << ans << "\n";
}

D. 生日相遇问题

题意

两个人的生日分别是 \(m_1\)\(d_1\) 日、 \(m_2\)\(d_2\) 日,问从2025年开始,连续 \(k\) 年里,他们生日在同一个星期几的年份有哪些。

题解

由于 \(k\) 只有50,所以就是模拟。首先将 2.29 变成 2.28 并打个tag:如果现在是闰年,那么临时把有tag的人的生日延后一天,判断之后立即把这一天“还回来”。然后把生日分为 1.1 - 2.28 和 3.1 - 12.31 两部分考虑,就做完了。

#include <bits/stdc++.h>
#define int long long
constexpr int N = 1e6 + 5;
constexpr int INF = 2e16;

int k, m1, d1, m2, d2;
int d[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool check(int x) {
    return (x % 100 == 0) ? (x % 400 == 0) : (x % 4 == 0);
}

void solve() {
    std::cin >> m1 >> d1 >> m2 >> d2 >> k;
    for (int i = 2; i <= 12; i++) d[i] += d[i - 1];
    int x = d[m1 - 1] + d1, y = d[m2 - 1] + d2, cur = 2025;
    bool xtag = 0, ytag = 0, ans = 0;
    if (m1 == 2 && d1 == 29) x--, xtag = 1;
    if (m2 == 2 && d2 == 29) y--, ytag = 1;
    while (k--) {
        bool chk = check(cur);
        if (chk) {
            if (m1 > 2) x++;
            if (m2 > 2) y++;
            x += xtag, y += ytag;
        }
        if (std::abs(x - y) % 7 == 0) {
            std::cout << cur << "\n";
            ans = 1;
        }
        if (chk) {
            if (m1 > 2) x--;
            if (m2 > 2) y--;
            x -= xtag, y -= ytag;
        }
        cur++;
    }
    if (!ans) std::cout << "No Answer\n";
}

E. 树

施工中

F. 连锁反应

施工中

posted on 2025-06-22 00:20  C12AK  阅读(81)  评论(1)    收藏  举报