Olympiad in Informatics

[Miscellaneous Problems] - December 9, 2019

Lately I've been learning some tech and doing some template problems. Now I'll gather a few problems I personally think are fairly good as the first miscellaneous problem set of December.

Subset

First, let's consider the case of odd-length segments (\(\{a_n\}\) is already sorted): fix the median as \(a_{mid}\), and we find that the answer is (the sum of everything on the left minus the median) plus (the sum of everything on the right minus the median), divided by the number of elements. To maximize the value, the right side obviously has to be chosen close to the right endpoint, while the left side has to be chosen close to the median. At this point we can consider using binary search on the answer to optimize: it cuts down the complexity of enumerating the length.

Why is this check binary-searchable? Since \(\forall a_i, a_i \geq 0\), we write \(suf_i^n = \sum_{j = i}^n a_j, suf_i^{mid} = \sum_{j = i}^{mid} a_j\). What we can be sure of is that the smaller \(i\) gets, both of these suffix sums are monotonically non-decreasing; also, writing \(\Delta_i^{mid} = suf_i^{mid} - suf_{i + 1}^{mid}, \Delta_i^n = suf_i^n - suf_{i + 1}^n\), we have \(\Delta_i^{mid} < \Delta_j^n, i \leq mid < j \leq n\). So we find that when running \(check(mid)\) with the median at \(a_i\), if the value of \(suf_{n - mid + 1}^n - suf_{i - mid}^{i}\) is smaller than the previous one, there is no need to extend any further.

The even-length case is the same as the odd one; if you need further explanation, remind me in the comments.

// YBT-OJ1679.cpp
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int MAX_N = 2e5 + 200;

int n, seq[MAX_N];
ll prefix[MAX_N];

double calc(int pos, int len) { return double(prefix[n] - prefix[n - len] + prefix[pos] - prefix[pos - len - 1]) / double((len << 1) | 1); }

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &seq[i]);
    sort(seq + 1, seq + 1 + n);
    for (int i = 1; i <= n; i++)
        prefix[i] = 1LL * prefix[i - 1] + seq[i];
    // odd one;
    double gans = 0;
    for (int i = 2; i <= n - 1; i++)
    {
        int l = 1, r = min(n - i, i - 1);
        double pans = seq[i];
        while (l <= r)
        {
            int len = (l + r) >> 1;
            double tmp = calc(i, len);
            double cp = calc(i, len - 1);
            if (tmp - cp > -double(1e-8))
                pans = tmp, l = len + 1;
            else
                r = len - 1;
        }
        gans = max(gans, pans - seq[i]);
    }
    printf("%.5lf\n", gans);
    return 0;
}

CF1257E - The Contest

This problem is okay, I guess. You can roughly lay things out over the value range and do prefix sums to take the minimum: if some value needs to be moved to the front, then everything smaller than it needs to be moved too. Prefix sums are enough.

// CF1257E.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e5 + 2000;

int k[4], n, prefix[4][MAX_N], seq[4][MAX_N], min_val[MAX_N];

int main()
{
    scanf("%d%d%d", &k[1], &k[2], &k[3]), n = k[1] + k[2] + k[3];
    for (int i = 1; i <= 3; i++)
        for (int j = 1; j <= k[i]; j++)
            scanf("%d", &seq[i][j]), prefix[i][seq[i][j]]++;
    for (int i = 1; i <= 3; i++)
        for (int j = 1; j <= n; j++)
            prefix[i][j] += prefix[i][j - 1];
    for (int i = 0; i <= n; i++)
        min_val[i] = prefix[3][i] - prefix[2][i];
    for (int i = n - 1; i >= 0; i--)
        min_val[i] = min(min_val[i + 1], min_val[i]);
    int ans = 0x3f3f3f3f;
    for (int i = 0; i <= n; i++)
        ans = min(ans, prefix[2][i] - prefix[1][i] + min_val[i] + prefix[1][n] + prefix[2][n]);
    printf("%d\n", ans);
    return 0;
}

CF1151F - Sonya and Informatics

I think this problem is pretty nice.

How do we characterize such a 01 string? We scan once and count that there are \(c\) \(0\)s, and then we can use \(dp[i][j]\) to record: after the first \(i\) operations, among the first \(c\) positions there are \(j\) \(0\)s. We find that a state like this basically pins down the counting pattern: we don't need the exact shape of the sequence, because swaps don't involve the exact shape and only affect the \(01\) counts of a segment, so counting this characterizes the state perfectly.

See the code comments for the exact equations; matrix multiplication is also needed to speed up the recurrence.

// CF1151F.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 110, mod = 1e9 + 7;

int n, k, seq[MAX_N], c;

struct matrix
{
    int mat[101][101];

    void clear() { memset(mat, 0, sizeof(mat)); }

    matrix operator*(const matrix &rhs)
    {
        matrix ret;
        ret.clear();
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= n; j++)
                for (int k = 0; k <= n; k++)
                    ret.mat[i][j] = (1LL * ret.mat[i][j] + 1LL * mat[i][k] * rhs.mat[k][j] % mod) % mod;
        return ret;
    }

    matrix operator^(const int &ti)
    {
        int tim = ti;
        matrix ret, bas = *this;
        ret.clear();
        for (int i = 0; i <= n; i++)
            ret.mat[i][i] = 1;
        while (tim)
        {
            if (tim & 1)
                ret = ret * bas;
            bas = bas * bas;
            tim >>= 1;
        }
        return ret;
    }
} A, B;

int quick_pow(int bas, int tim)
{
    int ret = 1;
    while (tim)
    {
        if (tim & 1)
            ret = 1LL * ret * bas % mod;
        bas = 1LL * bas * bas % mod;
        tim >>= 1;
    }
    return ret;
}

int main()
{
    const int inv2 = quick_pow(2, mod - 2);
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
        scanf("%d", &seq[i]), c += (seq[i] == 0);
    // dp[i][j] = dp[i - 1][j] * (j * (c - j) + (c - j) * (n - 2 * c + j) + (c * (c - 1) >> 1) + ((n - c) * (n - c - 1) >> 1))
    //         += dp[i - 1][j - 1] * (c - j + 1) * (c - j + 1)
    //         += dp[i - 1][j + 1] * (j + 1)* (n - 2 * c + j + 1);
    int preZero = 0;
    for (int i = 1; i <= c; i++)
        preZero += (seq[i] == 0);
    for (int j = 0; j <= c; j++)
    {
        if (j != 0)
            B.mat[j - 1][j] = 1LL * (c - j + 1) * (c - j + 1) % mod;
        B.mat[j][j] = (1LL * j * (c - j) % mod + 1LL * (c - j) * (n - 2 * c + j) % mod + 1LL * (1LL * c * (c - 1) % mod * inv2 % mod) + (1LL * (n - c) * (n - c - 1) % mod * inv2 % mod)) % mod;
        if (j != c)
            B.mat[j + 1][j] = 1LL * (j + 1) * (n - 2 * c + j + 1) % mod;
    }
    A.mat[0][preZero] = 1;
    B = B ^ k, A = A * B;
    int ans = A.mat[0][c], tot = 0;
    for (int i = 0; i <= c; i++)
        tot = (1LL * tot + A.mat[0][i]) % mod;
    printf("%lld\n", 1LL * ans * quick_pow(tot, mod - 2) % mod);
    return 0;
}