AtCoder Beginner Contest 094

A - Cats and Dogs

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int a, b, c;
int main(){
    cin >> a >> b >> c;
    if (c >= a && c <= a + b) cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return 0;
}

B - Toll Gates

长度为n+1的路上有m个收费站,每个收费站收费1元,现在从位置x出发,问走到0或者走到n位置,哪个方案收费最少

直接扫一遍即可

#include <bits/stdc++.h>

using namespace std;

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

C - Many Medians

给出n个数(n为偶数),要求对于每个数,都输出除了它以外,n-1个数的中位数

首先求出这n个数排序后最中间的两个数,答案肯定是这两个数中的一个

假设小的那个是res1,大的是res2,那么对于每个数来说,如果这个数小于等于res1,那么去除掉它后,中位数肯定是res2

反之,答案为res1

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int n, a[N], b[N];
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i];
    sort(a + 1, a + 1 + n);
    int res1 = a[n / 2], res2 = a[n / 2 + 1];
    for (int i = 1; i <= n; i++) {
        if (b[i] <= res1)
            cout << res2 << endl;
        else
            cout << res1 << endl;
    }
    return 0;
}

D - Binomial Coefficients

给出n个数,要求从中选择两个数,使得(c_a^b)最大

猜结论:a必然是最大值,b就选择最接近a的一半的那个值即可

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int n, a[N];
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a, a + n);
    LL res = 0;
    for (int i = 0; i < n - 1; i++) {
        if (abs(a[n - 1] - 2 * a[i]) < abs(a[n - 1] - 2 * res)) res = a[i];
    }
    cout << a[n - 1] << ' ' << res << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/14375696.html