Codeforces Round #772 (Div. 2) A - E

没写思路,等我补完E再说吧

A. Min Or Sum

思路

签到

#include <bits/stdc++.h>
using namespace std;
int num[40];
int main() {
    int t;
    scanf("%d", &t);
    while (t -- ) {
        for (int i = 0; i < 30; i ++ ) num[i] = 0;
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i ++ ) {
            int a;
            scanf("%d", &a);
            int pos = 0;
            while (a) {
                if (a & 1) num[pos] = 1;
                pos ++;
                a >>= 1;
            }
        }
        int ans = 0;
        for (int i = 0; i < 30; i ++ )
            if (num[i]) ans += (1 << i);
        cout << ans << endl;
    }
    return 0;
}

B. Avoid Local Maximums

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N];
int main() {
    int t;
    scanf("%d", &t);
    while (t -- ) {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i ++ ) scanf("%d", a + i);
        int cnt = 0;
        for (int i = 2; i < n; i ++ ) {
            if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
                if (i + 2 <= n) a[i + 1] = max(a[i], a[i + 2]);
                else a[i + 1] = a[i];
                cnt ++;
            }
        }
        printf("%d\n", cnt);
        for (int i = 1; i <= n; i ++ )
            printf("%d ", a[i]);
        puts("");
    }
    return 0;
}

C. Differential Sorting

#include <bits/stdc++.h>
using namespace std;
#define FI first
#define SE second
#define PB push_back
typedef pair<int, int> PII;
const int N = 2e5 + 10;
int a[N];
PII sum_mx[N], sum_mn[N];
struct Node {
    int x, y, z;
};
int main() {
    int t;
    scanf("%d", &t);
    while (t -- ) {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i ++ ) scanf("%d", a + i);
        if (a[n] < a[n - 1]) {
            printf("-1\n");
            continue;
        } 
        sum_mn[n].FI = 1e9 + 10;
        sum_mx[n].FI = -1e9 - 10, sum_mx[n - 1].FI = -1e9 - 10;
        for (int i = n - 1; i; i -- ) {
            if (a[i + 1] <= sum_mn[i + 1].FI) {
                sum_mn[i] = {a[i + 1], i + 1};
            } else {
                sum_mn[i] = sum_mn[i + 1];
            }
            if (i == n - 1) continue;
            if (a[i + 2] >= sum_mx[i + 2].FI) {
                sum_mx[i] = {a[i + 2], i + 2};
            } else {
                sum_mx[i] = sum_mx[i + 2];
            }
        }
        vector<Node> res;
        int pos = -1;
        for (int i = n - 2; i; i -- ) {
            if (sum_mn[i].FI - sum_mx[i].FI <= a[i + 1]) {
                pos = i;
                break;
            }
        }
        for (int i = 1; i <= pos; i ++ ) {
            res.PB({i, sum_mn[pos].SE, sum_mx[pos].SE});
            a[i] = sum_mn[pos].FI - sum_mx[pos].FI;
        }
        bool ok = 1;
        for (int i = 2; i <= n; i ++ )
            if (a[i] < a[i - 1]) {
                ok = 0;
                break;
            }
        if (!ok) {
            printf("-1\n");
            continue;
        }
        printf("%d\n", res.size());
        for (int i = 0; i < res.size(); i ++ )
            printf("%d %d %d\n", res[i].x, res[i].y, res[i].z);
    }
    return 0;
}

D. Infinite Set

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int dp[N];
int a[N];
int num[N];
int n, p;
int main() {
    scanf("%d%d", &n, &p);
    for (int i = 1; i <= n; i ++ ) 
        scanf("%d", a + i);
    sort(a + 1, a + n + 1);
    set<int> st;
    for (int i = 1; i <= n; i ++ ) {
        if (p <= 30 && a[i] >= (1 << p)) break;
        int tmp = a[i];
        bool ok = 1;
        while ((tmp % 2 == 1 || tmp % 4 == 0) && tmp) {
            if (tmp % 2 == 1) tmp /= 2;
            else tmp /= 4;
            if (st.count(tmp)) {
                ok = 0;
                break;
            }
        }
        if (ok) st.insert(a[i]);
    }
    for (auto t : st) {
        int tmp = t;
        int cnt = 0;
        while (tmp) {
            tmp >>= 1;
            cnt ++;
        }
        num[cnt - 1] ++;
    }
    dp[0] = num[0];
    dp[1] = num[1] + dp[0];
    for (int i = 2; i < p; i ++ ) 
        dp[i] = ((dp[i - 1] + dp[i - 2]) % mod + num[i]) % mod;
    int ans = 0;
    for (int i = 0; i < p; i ++ ) 
        ans = (ans + dp[i]) % mod;
    cout << ans << endl;
    return 0;
}
上一篇:Python3多思路解决空瓶换酒问题


下一篇:算法训练 反置数 java 题解 93