Olympiad in Informatics

Lyft Level 5 Challenge 2018 - Final Round (Open Div. 1) - Problem-Solving Report

A - The Tower is Going Home

It turns out that only the horizontal lines starting from the far left can contribute. When computing the answer, consider enumerating the vertical lines we cross, then update the answer.

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

using namespace std;

const int MAX_N = 1e5 + 200;

int n, m, ai[MAX_N], bi[MAX_N], cnt, ans;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]);
    sort(ai + 1, ai + 1 + n), ai[n + 1] = ans = 1e9;
    for (int i = 1, xl, xr, y; i <= m; i++)
    {
        scanf("%d%d%d", &xl, &xr, &y);
        if (xl == 1)
            bi[++cnt] = xr;
    }
    sort(bi + 1, bi + 1 + cnt);
    for (int i = 1; i <= n + 1; i++)
        ans = min(ans, int(i + cnt - (lower_bound(bi + 1, bi + 1 + cnt, ai[i]) - bi)));
    printf("%d\n", ans);
    return 0;
}

B - Intersecting Subtrees

IQ avalanche, sigh.

Actually, just pick any Y point, then find the nearest X point.

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

using namespace std;

const int MAX_N = 1010;

int T, n, head[MAX_N], current, k1, k2, p1[MAX_N], p2[MAX_N], dist[MAX_N];
bool tag1[MAX_N], tag2[MAX_N];

struct edge
{
    int to, nxt;
} edges[MAX_N << 1];

void addpath(int src, int dst)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    head[src] = current++;
}

int query(bool typ, int x)
{
    printf("%c %d\n", typ ? 'B' : 'A', x), fflush(stdout);
    scanf("%d", &x);
    return x;
}

int dfs(int u, int fa)
{
    if (tag1[u])
        return u;
    int ret = 0;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
        {
            dist[edges[i].to] = dist[u] + 1;
            int tmp = dfs(edges[i].to, u);
            if (dist[tmp] < dist[ret])
                ret = tmp;
        }
    return ret;
}

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        memset(head, -1, sizeof(head)), current = 0, memset(dist, 0, sizeof(dist));
        memset(tag1, false, sizeof(tag1)), memset(tag2, false, sizeof(tag2)), dist[0] = 0x3f3f3f3f;
        scanf("%d", &n);
        for (int i = 1, u, v; i < n; i++)
            scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
        scanf("%d", &k1);
        for (int i = 1; i <= k1; i++)
            scanf("%d", &p1[i]), tag1[p1[i]] = true;
        scanf("%d", &k2);
        for (int i = 1; i <= k2; i++)
            scanf("%d", &p2[i]), tag2[p2[i]] = true;
        int center = query(true, p2[1]), pt = dfs(center, 0);
        if (tag2[query(false, pt)])
            printf("C %d\n", pt), fflush(stdout);
        else
            puts("C -1"), fflush(stdout);
    }
    return 0;
}

C - Optimal Polygon Perimeter

Dammit, why is this whole round problems like this?

It turns out that when \(k \geq 4\), the Manhattan perimeter is really just the perimeter of the bounding rectangle. We only need to compute the maximum answers for the triangle and the quadrilateral.

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

using namespace std;

const int MAX_N = 3e5 + 200;

typedef long long ll;

int n;
ll xi[MAX_N], yi[MAX_N], rect[4][2];

ll dist(int x, int y) { return abs(rect[x][0] - rect[y][0]) + abs(rect[x][1] - rect[y][1]); }

ll distRPT(int x, int y) { return abs(rect[x][0] - xi[y]) + abs(rect[x][1] - yi[y]); }

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld%lld", &xi[i], &yi[i]);
        if (i == 1)
            for (int j = 0; j < 4; j++)
                rect[j][0] = xi[i], rect[j][1] = yi[i];
        else
        {
            if (xi[i] > rect[0][0])
                rect[0][0] = xi[i], rect[0][1] = yi[i];
            if (yi[i] < rect[1][1])
                rect[1][0] = xi[i], rect[1][1] = yi[i];
            if (xi[i] < rect[2][0])
                rect[2][0] = xi[i], rect[2][1] = yi[i];
            if (yi[i] > rect[3][1])
                rect[3][0] = xi[i], rect[3][1] = yi[i];
        }
    }
    ll ans = 0;
    for (int i = 0; i <= 3; i++)
        for (int j = 0; j <= 3; j++)
            if (i != j)
                for (int k = 1; k <= n; k++)
                    ans = max(ans, dist(i, j) + distRPT(i, k) + distRPT(j, k));
    printf("%lld ", ans), ans = 0;
    for (int i = 0; i <= 3; i++)
        ans += dist(i, (i + 1) % 4);
    for (int i = 4; i <= n; i++)
        printf("%lld ", ans);
    puts("");
    return 0;
}

D - Deduction Queries

A weighted disjoint-set union (DSU) problem (I didn’t really know it before).

We consider describing this problem through relations between prefix sums. For an operation that updates an interval, we can merge \(r\) onto \(l - 1\) and assign a weight. For a query operation, if the two are in the same connected component, the \(\text{xor}\) sum along the path is the answer; otherwise it cannot be described.

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

using namespace std;

const int MAX_N = 2e5 + 200;

int q, lastans;
map<int, int> fa, weight;

int find(int x)
{
    if (fa.count(x) == 0)
        return x;
    int up = find(fa[x]);
    weight[x] ^= weight[fa[x]];
    fa[x] = up;
    return up;
}

int main()
{
    scanf("%d", &q);
    while (q--)
    {
        int opt, l, r, x;
        scanf("%d", &opt);
        if (opt == 1)
        {
            scanf("%d%d%d", &l, &r, &x), l ^= lastans, r ^= lastans, x ^= lastans;
            if (l > r)
                swap(l, r);
            l--;
            int lf = find(l), rf = find(r);
            if (lf != rf)
                fa[rf] = lf, weight[rf] = x ^ weight[l] ^ weight[r];
        }
        else
        {
            scanf("%d%d", &l, &r), l ^= lastans, r ^= lastans;
            if (l > r)
                swap(l, r);
            l--;
            int lf = find(l), rf = find(r);
            if (lf != rf)
                puts("-1"), lastans = 1;
            else
                printf("%d\n", lastans = weight[l] ^ weight[r]);
        }
    }
    return 0;
}