Olympiad in Informatics

[Fortuna OJ] Mar 2nd - Group A Problem-Solving Report

I was in terrible shape for today's contest: sleepy, hungry, and my eyes were dry.

A - Rescuing the Cows

We first convert the problem into the distance between two points on a triangular matrix. By analogy with Manhattan distance, we can split the distance into two kinds, vertical and horizontal, and consider them separately.

First, the vertical part. If \((x_1,y_1)\) wants to get to \((x_2,y_2)\), there are the following cases:

  • If \(x_1\) and \(x_2\) have different parities, the contribution is \(2|x_1-x_2|-1\).
  • If they are the same, the contribution is \(2|x_1-x_2|\).

Now let's look at the horizontal part. We notice that if we expand the triangle above like this:

Figure on Problem A, March 2nd

We find that the triangles within this range need no extra horizontal contribution; computing only the vertical contribution gives the answer. For triangles in the same row but outside this region, the horizontal contribution is also very easy to compute: just take the difference and multiply by two.

Remember to sort the input points.

Code

// A.cpp
#include <bits/stdc++.h>
#define pr pair<int, int>
using namespace std;
const int MAX_N = 1001000;
pr prs[MAX_N];
int n, m, si, sj;
int answer, exitI, exitJ;
int main()
{
    answer = 0x3f3f3f3f;
    scanf("%d%d%d%d", &m, &n, &si, &sj);
    for (int i = 1; i <= n; i++)
        scanf("%d%d", &prs[i].first, &prs[i].second);
    sort(prs + 1, prs + 1 + n);
    for (int i = 1; i <= n; i++)
    {
        pr pta = prs[i], ptb = make_pair(si, sj);
        if (pta.first < ptb.first)
            swap(pta, ptb);
        int jl = ptb.second, jr = ptb.second + (pta.first - ptb.first) * 2;
        int ans = (pta.first - ptb.first) << 1;
        if (pta.second >= jl && pta.second <= jr &&
            (ans < answer || (ans == answer && exitJ >= prs[i].second)))
        {
            answer = ans, exitI = prs[i].first, exitJ = prs[i].second;
            if ((pta.second & 1) != (ptb.second & 1))
                answer -= 1;
            continue;
        }
        ans += min(abs(pta.second - jl), abs(pta.second - jr));
        if (ans < answer || (ans == answer && exitJ >= prs[i].second))
            answer = ans, exitI = prs[i].first, exitJ = prs[i].second;
    }
    printf("%d %d\n%d", exitI, exitJ, answer + 1);
    return 0;
}

B - The Postman

First of all, the \(w_i\) in this problem is completely damn useless. Let's look at this \(w_i\)'s contribution to the loss:

\[ \sum_{i=1}^{n}( i-w_{s_i} )= \frac{n(n-1)}{2}-\sum_{i=1}^{n} w_i \]

So the order has no effect whatsoever. So we look for the shortest path that can be drawn in one stroke, with the smallest lexicographic order, which guarantees the simplest answer.

Because the problem clearly says (too bad I didn't see it; I must be blind)

The number of roads by which you can leave each village is always 2, 4 or 8.

Boy, am I stupid.

So, store the graph in an adjacency list, then run a DFS in increasing order of labels, pushing onto a stack, and finally pop the stack in reverse to output.

Code

// B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 220;
int n, m, wi[MAX_N], tmpx, tmpy, dist[MAX_N][MAX_N], tot;
stack<int> stk;
void dfs(int u)
{
    for (int i = 1; i <= n; i++)
        if (dist[u][i])
        {
            dist[u][i]--, dist[i][u]--;
            dfs(i);
        }
    stk.push(u);
}
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &wi[i]);
    for (int i = 1; i <= m; i++)
        scanf("%d%d", &tmpx, &tmpy), dist[tmpx][tmpy]++, dist[tmpy][tmpx]++;
    dfs(1);
    printf("%d\n", stk.size() - 1);
    while (!stk.empty())
        printf("%d ", stk.top()), stk.pop();
    return 0;
}

C - Minimum Density Path

We can consider defining the state \(f[i][j][k]\) as the total length of going from node \(i\) to \(j\) along \(k\) edges, then preprocessing with Floyd, and finally answering in \(O(n)\). A stupid-easy problem.

Code

// C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 55, MAX_M = 1200;
int n, m, tmpx, tmpy, tmpz, f[MAX_N][MAX_N][MAX_M], dist[MAX_N][MAX_N], q;
int main()
{
    scanf("%d%d", &n, &m);
    memset(f, 0x3f, sizeof(f)), memset(dist, 0x3f, sizeof(dist));
    for (int i = 1; i <= m; i++)
        scanf("%d%d%d", &tmpx, &tmpy, &tmpz), dist[tmpx][tmpy] = min(dist[tmpx][tmpy], tmpz);
    for (int i = 1; i <= n; i++)
        f[i][i][0] = 0;
    for (int s = 1; s <= n; s++)
        for (int k = 1; k <= n; k++)
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= n; j++)
                    if (f[i][j][s] > f[i][k][s - 1] + dist[k][j])
                        f[i][j][s] = f[i][k][s - 1] + dist[k][j];
    scanf("%d", &q);
    while (q--)
    {
        double ans = (double)0x3f3f3f3f;
        int i, j;
        bool flag = true;
        scanf("%d%d", &i, &j);
        for (int s = 1; s <= n; s++)
            if (1.0 * f[i][j][s] / (1.0 * s) < ans && f[i][j][s] != (double)0x3f3f3f3f)
                ans = min(ans, 1.0 * f[i][j][s] / (1.0 * s)), flag = false;
        if (flag)
            puts("OMG!");
        else
            printf("%.3lf\n", ans);
    }
    return 0;
}