Olympiad in Informatics

[Fortuna OJ] Feb 16th – Group B Problem-Solving Report

A - Binary & B - Chess

Idiotic problems, no analysis.

C - Sum

This problem is really interesting (after careful parameter tuning, a randomized program can get 90 points), and the intended solution is really clever. We can reduce the problem to finding \(min\{ S_{i,j} \mod P, K \leq S_{i,j} \mod P\}\). We first spend \(O(n)\) time precomputing the prefix sums, then we process the prefix sums in reverse order, inserting them into a set from back to front. While inserting into the set, we binary search for the two target optimal solutions \(s[i]+k\) and \(s[i]+k-p\), then just record the answer. Really clever.

// C.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 100010;
int n, k, p, ans = 0x7f7f7f7f, s[MAX_N];
set<int> st;
int main()
{
    scanf("%d%d%d", &n, &k, &p);
    for (int i = 1; i <= n; i++)
        scanf("%d", &s[i]), (s[i] += s[i - 1]) %= p;
    set<int>::iterator it;
    st.insert(s[n]);
    for (int i = n - 1; i >= 1; i--)
    {
        it = st.lower_bound(s[i] + k);
        if (it != st.end())
            ans = min(*it - s[i], ans);
        it = st.lower_bound(s[i] + k - p);
        if (it != st.end() && *it < s[i])
            ans = min(*it - s[i] + p, ans);
        st.insert(s[i]);
    }
    printf("%d", ans);
    return 0;
}

D - Jail

Oh, an idiotic problem. —crazydave

This one counts as a standard trick, I guess: use a bitmask to enumerate the sign state of each dimension, find the maximum and the minimum, and take the maximum minus the minimum (the minimum stands for the Manhattan contribution of the opposite state). That’s it.

// D.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000010;
int n, d, info[MAX_N][6], st[10], ans;
void calc(int stat)
{
    int mn = (1 << 29), mx = -mn;
    for (int i = 1; i <= d; i++, stat >>= 1)
        st[i] = stat & 1;
    for (int i = 1; i <= n; i++)
    {
        int acc = 0;
        for (int j = 1; j <= d; j++)
            acc += (st[j] == 1) ? info[i][j] : -info[i][j];
        if (acc != 0)
            mn = min(mn, acc), mx = max(mx, acc);
    }
    ans = max(mx - mn, ans);
}
int main()
{
    scanf("%d%d", &n, &d);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= d; j++)
            scanf("%d", &info[i][j]);
    for (int i = 0; i < (1 << d); i++)
        calc(i);
    printf("%d", ans);
    return 0;
}