「蓝桥·算法双周赛」第 3 场 算法季度赛

1. 全国科普行动日【算法赛】

#include <iostream>
using namespace std;
int main()
{
  cout << "6.29";
  return 0;
}

2. A%B【算法赛】

#include <bits/stdc++.h>

using namespace std;


using i32 = int32_t;
using i64 = long long;
using i128 = __int128;

#define int i64

using vi = vector<int>;
using pii = pair<int,int>;

const int inf = 1e9, INF = 1e18;

void solve(){
    int a, b;
    cin >> a >> b;
    cout << (a % b + abs(b)) % abs(b) << "\n";
    return ;
}

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int T;
    cin >> T;
    while(T --) solve();
    return 0;
}

3. 能量圆盘【算法赛】

选择出现次数最多的宝石保留。

#include <bits/stdc++.h>

using namespace std;


using i32 = int32_t;
using i64 = long long;
using i128 = __int128;

#define int i64

using vi = vector<int>;
using pii = pair<int, int>;

const int inf = 1e9, INF = 1e18;


i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n;
    cin >> n;
    map<int, int> cnt;
    for (int i = 1, x; i <= n; i++)
        cin >> x, cnt[x]++;
    int ans = -1;
    for (auto [k, v]: cnt) ans = max(ans, v);
    cout << n - ans << "\n";
    return 0;
}

4. 植物保留【算法赛】

枚举保留每一个植物,二分找到前面最靠后可以放的。

#include <bits/stdc++.h>

using namespace std;


using i32 = int32_t;
using i64 = long long;
using i128 = __int128;

#define int i64

using vi = vector<int>;
using pii = pair<int, int>;

const int inf = 1e9, INF = 1e18;


i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vi a(n);
    for (auto &i: a) cin >> i;
    vi f(n);
    f[0] = 1;
    for (int i = 1, j; i < n; i++) {
        if (a[i] - m < a[0]) f[i] = 1;
        else {
            j = upper_bound(a.begin(), a.end(), a[i] - m) - a.begin() - 1;
            f[i] = f[j] + 1;
        }
        f[i] = max(f[i], f[i - 1]);
    }
    cout << f.back() << "\n";

    return 0;
}

5. 龙骑士军团【算法赛】

前缀和一下,然后区间最值查询

#include <bits/stdc++.h>

using namespace std;


using i32 = int32_t;
using i64 = long long;
using i128 = __int128;

#define int i64

using vi = vector<int>;
using pii = pair<int, int>;

const int inf = 1e9, INF = 1e18;


const int N = 2e5 + 5, logN = 20;
int a[N], log_2[N], f[N][logN + 1], g[N][logN + 1];

int queryMax(int l, int r) {
    int s = log_2[r - l + 1];
    return max(f[l][s], f[r - (1 << s) + 1][s]);
}

int queryMin(int l, int r) {
    int s = log_2[r - l + 1];
    return min(g[l][s], g[r - (1 << s) + 1][s]);
}

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    n++;
    for (int i = 2; i <= n; i++)
        cin >> a[i], a[i] += a[i - 1];

    log_2[0] = -1;
    for (int i = 1; i <= n; i++)
        f[i][0] = g[i][0] = a[i], log_2[i] = log_2[i >> 1] + 1;
    for (int j = 1; j <= logN; j++)
        for (int i = 1; i + (1 << j) - 1 <= n; i++) {
            f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
            g[i][j] = min(g[i][j - 1], g[i + (1 << j - 1)][j - 1]);
        }
    for (int a, b, c, d; q; q--) {
        cin >> a >> b >> c >> d, c++, d++;
        cout << queryMax(c, d) - queryMin(a, b) << "\n";
    }

    return 0;
}

6. 热身操领队【算法赛】

动态的中位数查询。

#include <bits/stdc++.h>

using namespace std;


using i32 = int32_t;
using i64 = long long;
using i128 = __int128;

#define int i64

using vi = vector<int>;
using pii = pair<int, int>;

const int inf = 1e9, INF = 1e18;


const int N = 2e5 + 5, logN = 20;

#define lowbit(x) ( x & -x )

struct BinaryIndexedTree {
    int n;
    vector<int> b;

    BinaryIndexedTree(int n) : n(n), b(n + 1, 0) {};

    void add(int i, int y) {
        for (; i <= n; i += lowbit(i)) b[i] += y;
        return;
    }

    int calc(int i) {
        int sum = 0;
        for (; i; i -= lowbit(i)) sum += b[i];
        return sum;
    }

    int find(int k) {
        int l = 1, r = n, ans = -1;
        for (int mid; l <= r;) {
            mid = (l + r) / 2;
            if (calc(mid - 1) * 2 < k) ans = mid, l = mid + 1;
            else r = mid - 1;
        }
        return ans;
    }

};

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n;
    cin >> n;
    vector<pii> op(n);
    vi h;
    for (auto &[a, v]: op)
        cin >> a >> v, h.push_back(a);
    sort(h.begin(), h.end());
    for (auto &[a, v]: op)
        a = lower_bound(h.begin(), h.end(), a) - h.begin() + 1;
    BinaryIndexedTree bit(n);
    int sum = 0;
    for (auto [a, v]: op) {
        bit.add(a, v) , sum += v;
        int i = bit.find( sum );
        cout << h[i - 1] << "\n";
    }
    return 0;
}

7. 单词博弈【算法赛】

很奇妙的解法,贪心的选择可用的最小值

#include <bits/stdc++.h>

using namespace std;


using i32 = int32_t;
using i64 = long long;
using i128 = __int128;

#define int i64

using vi = vector<int>;
using pii = pair<int, int>;

const int inf = 1e9, INF = 1e18;


i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    set<string> a[2];
    string lst, s;
    for (int i = 1; i <= n; i++) {
        cin >> s, a[0].insert(s);
    }
    for (int i = 1; i <= m; i++) {
        cin >> s, a[1].insert(s);
    }
    lst = *a[0].begin(), a[0].erase(lst);
    for (int i = 1;; i ^= 1) {
        while (not a[i].empty() and *a[i].begin() <= lst)
            a[i].erase(a[i].begin());
        if (a[i].empty() or a[i].begin()->front() - lst.front() > 1) {
            cout << "QL"[i];
            break;
        }
        lst = *a[i].begin() , a[i].erase(lst);
    }
    return 0;
}
posted @ 2024-06-30 22:54  PHarr  阅读(13)  评论(0编辑  收藏  举报