代码源挑战赛 Round 2

#7. [R2A]三人组队

#include <bits/stdc++.h>

using i64 = int64_t;
using vi = std::vector<i64>;
using std::cin, std::cout;


int main() {
    std::ios::sync_with_stdio(false), cin.tie(nullptr);
    int n;
    cin >> n;
    vi a(n);
    for(auto &i : a) cin >> i;
    std::ranges::sort(a, std::greater<>());
    cout << a[0] + a[1] + a[2];
    return 0;
}

#8. [R2B]向前看

维护前缀最大值即可。

#include <bits/stdc++.h>

using i64 = int64_t;
using vi = std::vector<i64>;
using std::cin, std::cout;


int main() {
    std::ios::sync_with_stdio(false), cin.tie(nullptr);
    int n;
    cin >> n;
    int res = 0, h = - 1;
    for(int i = 1, x; i <= n ; i ++ ){
        cin >> x;
        res += (h < x);
        h = std::max(h, x);
    }
    cout << res << "\n";
    return 0;
}

#9. [R2C]三元组

组合数

#include <bits/stdc++.h>

using i64 = int64_t;
using vi = std::vector<i64>;
using std::cin, std::cout;


int main() {
    std::ios::sync_with_stdio(false), cin.tie(nullptr);
    int n;
    cin >> n;
    std::map<int, int> cnt;
    for (int i = 0, x; i < n; i++) {
        cin >> x;
        cnt[x]++;
    }
    i64 res = 0;
    for(auto [k, v] : cnt) {
        if(v < 3) continue;
        res += (i64)v * (v - 1) * (v - 2) / (3 * 2 * 1);
    }
    cout << res;
    return 0;
}

#10. [R2D]倍数问题

先维护出每一个数出现的次数,然后用类埃筛的方法统计每个数字出现的次数。

#include <bits/stdc++.h>

using i64 = int64_t;
using vi = std::vector<i64>;
using std::cin, std::cout;

const int N = 5e5;

int main() {
    std::ios::sync_with_stdio(false), cin.tie(nullptr);
    int n;
    cin >> n;
    vi cnt(N + 1), res(N + 1);
    for (int i = 0, x; i < n; i++) {
        cin >> x;
        cnt[x]++;
    }

    res[1] = n;
    for(int i = 2; i <= N; i ++)
        for(int j = i; j <= N; j += i)
            res[i] += cnt[j];

    int q;
    cin >> q;
    for(int x; q; q--) {
        cin >> x;
        cout << res[x] << "\n";
    }
    return 0;
}

#11. [R2E]路线规划

必须要经过的点只有\(k + 1\)个,以这\(k + 1\)个点为起点求一下最短路,然后全排列找出最短路即可。

#12. [R2F]带修求和

维护一个\(cnt[i]\)表示数字\(i\)出现的次数。记\(p[i]=cnt[i]\times i\),对于询问2实际上就是最短前缀,询问3实际上最短后缀。

这个问题变成了单点修改,区间查询问题,可以用树状数组实现。

posted @ 2025-05-10 00:52  PHarr  阅读(77)  评论(0)    收藏  举报