Codeforces Round #703 (Div. 2)

Codeforces Round #703 (Div. 2)

A - Shifting Stacks

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; bool f = 1;
        rep (i, 1, n) {
            cin >> a[i], a[i] += a[i - 1];
            if (a[i] >= a[0]) a[0] += i;
            else f = 0;
        }
        cout << (f ? "YES
" : "NO
");
    }
    return 0;
}

B - Eastern Exhibition

x, y坐标中位数个数相乘

ll a[N], b[N];
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; ll x = 1, y = 1;
        rep (i, 1, n) cin >> a[i] >> b[i];
        sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + n);
        if (!(n & 1)) x = a[n + 2 >> 1] - a[n >> 1] + 1, y = b[n + 2 >> 1] - b[n >> 1] + 1;
        cout << x * y << '
';
    }
    return 0;
}

C1 - Guessing the Greatest

二分

int ask(int l, int r) {
    int x; cout << "? " << l << ' ' << r << endl;
    cout.flush();
    cin >> x; return x;
}
 
int main() {
    IOS; cin >> n; m = ask(1, n);
    if (m == n || ask(m, n) != m) {
        int l = 1, r = m - 1;
        while (l < r) {
            int mid = l + r + 1 >> 1;
            if (ask(mid, m) == m) l = mid;
            else r = mid - 1;
        }
        cout << "! " << l << '
';
    } else {
        int l = m + 1, r = n;
        while (l < r) {
            int mid = l + r >> 1;
            if (ask(m, mid) == m) r = mid;
            else l = mid + 1;
        }
        cout << "! " << r << '
';
    }
    return 0;
}

D - Max Median

二分

int a[N], b[N], c[N];
 
bool check(int mid) {
    rep (i, 1, n) b[i] = b[i - 1] + (a[i] >= mid ? 1 : -1), c[i] = min(c[i - 1], b[i]);
    rep (i, k, n) if (b[i] > c[i - k]) return 1;
    return 0;
}
 
int main() {
    IOS; cin >> n >> k;
    rep (i, 1, n) cin >> a[i];
    int l = 1, r = n;
    while (l < r) {
        int mid = l + r + 1 >> 1;
        if (check(mid)) l = mid;
        else r = mid - 1;
    }
    cout << l;
    return 0;
}
原文地址:https://www.cnblogs.com/2aptx4869/p/14418865.html