当前位置:网站首页>Codeforces Round #796 (Div. 2)(A-D)

Codeforces Round #796 (Div. 2)(A-D)

2022-07-31 15:05:00 eyuhaobanga

tnnd,为什么不AKdiv2?(还不是因为不会做+时间少,知识点还好多没学QAQ

Problem - A - Codeforces

AC代码:

#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
using namespace std;
using LL = long long;

void Solve() {
    int x;
    cin >> x;
    for (int i = 1; i <= 30; i++) {
        if (x == (1 << i)) {
            cout << x + 1 << "\n";
            return;
        }
    }
    int y = x & (-x);
    while ((y ^ x) == 0 || (y & x) == 0) {
        y++;
    }
    cout << y << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    rep (i, 0, T) {
        Solve();
    }

    return 0;
}

Problem - B - Codeforces

AC代码:

#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
using namespace std;
using LL = long long;

void Solve() {
    int n;
    cin >> n;
    int cnt = 0;
    vector<int> a(n);
    int ans = 0x3f3f3f3f;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        if (a[i] % 2 == 1) {
            cnt++;
        }
    }
    if (cnt) {
        ans = min(ans, n - cnt);
    }
    int sum = 0x3f3f3f3f;
    for (int i = 0; i < n; i++) {
        int x = 0;
        if (a[i] % 2 == 0) {
            while (a[i] % 2 != 1) {
                x++;
                a[i] >>= 1;
            }
        }
        sum = min(sum, x);
    }
    cout << min(ans, sum + n - 1) << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    rep (i, 0, T) {
        Solve();
    }

    return 0;
}

Problem - C - Codeforces

AC代码:

#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
using namespace std;
using LL = long long;

void Solve() {
    int n;
    cin >> n;
    n <<= 1;
    vector<string> a(n);
    map<char, int> mp;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        int len = a[i].size();
        for (int j = 0; j < len; j++) {
            mp[a[i][j]]++;
        }
    }
    string s;
    cin >> s;
    int len = s.size();
    for (int i = 0; i < len; i++) {
        mp[s[i]]++;
    }
    for (auto it : mp) {
        if (it.second % 2 == 1) {
            cout << it.first << '\n';
            return;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    rep (i, 0, T) {
        Solve();
    }

    return 0;
}

Problem - D - Codeforces

AC代码:

#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
using namespace std;
using LL = long long;

void Solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    vector<LL> sum(n);
    rep (i, 0, n) {
        cin >> a[i];
        if (i == 0) {
            sum[i] = a[i];
        }
        else {
            sum[i] = a[i] + sum[i - 1];
        }
    }
    int x = k / n;
    int y = k - x * n;
    LL ans = 0;
    if (x == 0) {
        for (int i = k - 1; i < n; i++) {
            if (i >= k) {
                ans = max(ans, sum[i] - sum[i - k]);
            }
            else {
                ans = max(ans, sum[i]);
            }
        }
        cout << ans + 1LL * k * (k - 1) / 2 << '\n';
        return;
    }
    for (int i = 0; i < n; i++) {
        ans += 1LL * a[i] + i;
    }
    x--;
    ans += 1LL * n * x * n;
    ans += 1LL * y * n;
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    rep (i, 0, T) {
        Solve();
    }

    return 0;
}

原网站

版权声明
本文为[eyuhaobanga]所创,转载请带上原文链接,感谢
https://blog.csdn.net/eyuhaobanga/article/details/126020823

随机推荐