AtCoder Beginner Contest 117

A - Entrance Examination

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
double a, b;
int main(){
    cin >> a >> b;
    cout << a / b << endl;
    return 0;
}

B - Polygon

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int n;
int a[N];
int main(){
    cin>>n;
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    int sum = 0;
    for (int i = 0; i < n - 1; i++) sum += a[i];
    if (a[n - 1] >= sum) cout << "No" << endl;
    else
        cout << "Yes" << endl;
    return 0;
}

C - Streamline

给出n个数,现在可以选m个点,每个点都可以不停地+1或者-1,走到其他的点,问在最佳策略下,用m个点走完n个点的步数最少是多少

贪心的想,直接删掉m-1个最长的区间即可

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int n, m,a[N],b[N];
LL res = 0;
int main(){
    cin >> m >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    for (int i = 1; i < n; i++) b[i - 1] = a[i] - a[i - 1];
    sort(b, b + n - 1);
    for (int i = 0; i < n - m; i++) res += b[i];
    cout << res << endl;
    return 0;
}

D - XXOR

给出n个数,和一个数k,问0到k中的数x,和这n个数的异或和最大是多少

从最高位考虑,记录这一位在n个数中出现的次数,如果出现的0比1多,那么x的这一位就是1,否则就是0,注意要看k的限制,看能不能取到1,如果前面已经在能取1的情况下取到了0,那么这一位必然可以取1 ,否则就要看k的这一位是不是1

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int n;
LL k, A[N];
int sum[N], maxbit, a[N];
int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        LL x;
        cin >> x;
        A[i] = x;
        int bit = 0;
        while (x) {
            sum[bit] += (x & 1);
            bit++;
            x /= 2;
        }
        maxbit = max(maxbit, bit);
    }
    int bit = 0;
    while (k) {
        a[bit] += (k & 1);
        bit++;
        k /= 2;
    }
    maxbit = max(maxbit, bit);
    bool flag = 0;
    LL x = 0;
    for (int i = maxbit - 1; i >= 0; i--) {
        if (sum[i] < n - sum[i]) {
            if (flag)
                x = (x << 1) + 1;
            else if (a[i])
                x = (x << 1) + 1;
            else
                x = (x << 1);
        } else {
            if (a[i]) flag = 1;
            x = (x << 1);
        }
    }
    LL res = 0;
    for (int i = 0; i < n; i++) {
        res += A[i] ^ x;
    }
    cout << res << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/14403240.html