Olympiad in Informatics

AtCoder Regular Contest 092 Problem-Solving Report

C - 2D Plane 2N Points

You can consider sorting first, then just handling it greedily, by brute force.

@@ -0,0 +1,36 @@
// ARC092A.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 210;

int n;
bool enabled[MAX_N];

struct point
{
    int x, y, typ;
    bool operator<(const point &rhs) const { return x < rhs.x; }
} pts[MAX_N];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= (n << 1); i++)
        scanf("%d%d", &pts[i].x, &pts[i].y), pts[i].typ = i > n;
    sort(pts + 1, pts + 1 + (n << 1));
    int ans = 0;
    for (int i = 1; i <= (n << 1); i++)
        if (pts[i].typ == 1)
        {
            int id = -1;
            for (int j = 1; j < i; j++)
                if (!enabled[j] && pts[j].typ == 0 && pts[j].y < pts[i].y && (id == -1 || pts[j].y > pts[id].y))
                    id = j;
            if (id != -1)
                enabled[id] = true, ans++;
        }
    printf("%d\n", ans);
    return 0;
}

D - Two Sequences

This problem is pretty interesting; it reminds me of a certain Div.1 B.

We can go from the low bits to the high bits. Let \(T = p^b, mod = 2T\). Then we first take all the \(a_i, b_i\) modulo \(mod\), and what we can notice is that, for a fixed \(b_i\), if \(T \leq a_i + b_i < 2T, 3T \leq a_i + b_i < 4T\), then the contribution of this bit is \(1\). After taking the modulo, we can binary search for these ranges and get the answer.

@@ -0,0 +1,38 @@
// ARC092B.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e5 + 200;

int n, ai[MAX_N], bi[MAX_N], ta[MAX_N], tb[MAX_N];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]);
    for (int i = 1; i <= n; i++)
        scanf("%d", &bi[i]);
    int ans = 0;
    for (int b = 0; b < 29; b++)
    {
        int T = 1 << b, mod = T << 1;
        for (int i = 1; i <= n; i++)
            ta[i] = ai[i] % mod, tb[i] = bi[i] % mod;
        sort(tb + 1, tb + 1 + n);
        int pans = 0;
        for (int i = 1; i <= n; i++)
        {
            int l = lower_bound(tb + 1, tb + 1 + n, T - ta[i]) - tb;
            int r = lower_bound(tb + 1, tb + 1 + n, mod - ta[i]) - tb;
            pans ^= (r - l) & 1;
            l = lower_bound(tb + 1, tb + 1 + n, T * 3 - ta[i]) - tb;
            r = lower_bound(tb + 1, tb + 1 + n, (mod << 1) - ta[i]) - tb;
            pans ^= (r - l) & 1;
        }
        ans += (pans << b);
    }
    printf("%d\n", ans);
    return 0;
}

E - Both Sides Merger

First, there is a property: the numbers that contribute to the final answer all have indices of the same parity. This is a necessary and sufficient condition, so we can just split into cases and get the final answer.

@@ -0,0 +1,47 @@
// ARC092C.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1010;

typedef long long ll;

int n, ai[MAX_N];
ll sum[2];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]), sum[i & 1] += max(0, ai[i]);
    if (max(sum[0], sum[1]) == 0)
    {
        int pos = max_element(ai + 1, ai + 1 + n) - ai;
        printf("%d\n%d\n", ai[pos], n - 1);
        for (int i = n; i > pos; i--)
            printf("%d\n", i);
        for (int i = pos - 1; i >= 1; i--)
            puts("1");
        return 0;
    }
    int parity = sum[1] >= sum[0];
    vector<int> pos, ans;
    for (int i = 1; i <= n; i++)
        if (ai[i] > 0 && i % 2 == parity)
            pos.push_back(i);
    for (int i = n; i > pos.back(); i--)
        ans.push_back(i);
    for (int i = 1; i < pos.front(); i++)
        ans.push_back(1);
    for (int i = 0, siz = pos.size(); i < siz - 1; i++)
    {
        for (int j = 0; j < ((pos[i + 1] - pos[i] - 1) >> 1); j++)
            ans.push_back(3);
        ans.push_back(2);
    }
    printf("%lld\n%lld\n", sum[parity], 1LL * ans.size());
    for (int v : ans)
        printf("%d\n", v);
    return 0;
}

F - Two Faced Edges

One brute-force approach is to just run Tarjan in \(\Theta(nm + m^2)\), but that goes gg. Let's think carefully about how the direction of an edge affects the SCCs. If reversing this edge neither forms a new SCC nor breaks an existing one, then this doesn't change.

We notice that an edge forming an SCC and an edge breaking an SCC are independent effects, so we consider them separately. An SCC is broken if and only if, after edge \((u, v)\) is reversed, \(u\) can no longer reach \(v\); that way we really have broken an SCC. We can actually get this by running Tarjan in different orders to obtain different dfn and low values, and from that tell whether there is a second path to take.

Then, as for forming an SCC, that happens if and only if \(b_i\) cannot reach \(a_i\). This works the same way. XOR the results of these two problems together and that's the answer.

@@ -0,0 +1,63 @@
// ARC092D.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1010, MAX_E = 2e5 + 200;

int n, m, mp[MAX_N][MAX_N], etot, ui[MAX_E], vi[MAX_E], p[MAX_N], q[MAX_N];
bool accessible[MAX_N][MAX_N], vis[MAX_N], enabled[MAX_N][MAX_N];
vector<int> G[MAX_N];

void mark(int u, int root)
{
    vis[u] = true, accessible[root][u] = true;
    for (int v : G[u])
        if (!vis[v])
            mark(v, root);
}

void markPos(int u)
{
    vis[u] = true;
    for (int v : G[u])
        if (!vis[v])
            p[v] = mp[u][v], markPos(v);
}

void markNeg(int u)
{
    vis[u] = true;
    int siz = G[u].size() - 1;
    for (int i = siz; i >= 0; i--)
    {
        int v = G[u][i];
        if (!vis[v])
            q[v] = mp[u][v], markNeg(v);
    }
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1, u, v; i <= m; i++)
    {
        scanf("%d%d", &u, &v);
        ui[++etot] = u, vi[etot] = v;
        mp[u][v] = etot, G[u].push_back(v);
    }
    for (int i = 1; i <= n; i++)
        mark(i, i), memset(vis, false, sizeof(vis));
    for (int i = 1; i <= n; i++)
    {
        markPos(i), memset(vis, false, sizeof(vis));
        markNeg(i), memset(vis, false, sizeof(vis));
        for (int v : G[i])
            if (p[v] != mp[i][v] || q[v] != mp[i][v])
                enabled[i][v] = true;
        memset(p, 0, sizeof(p)), memset(q, 0, sizeof(q));
    }
    for (int i = 1; i <= m; i++)
        puts((accessible[vi[i]][ui[i]] ^ enabled[ui[i]][vi[i]]) ? "diff" : "same");
    return 0;
}