---
id: 1534
lang: en
translation_of: index.md
source_sha256: 005a68060fbac0937e216b9b38f6b9f07716af48004b27ed5a219609a3bd418d
title: "[Fortuna OJ] Aug 2nd - Group A Problem-Solving Report / National Training Team Mutual Test 2012 [Li Chao]"
description: "A: brute force under a 10s limit. B: binary search on p, expectation DP and matrix exponentiation. C: extremes for the max, CDQ divide and conquer for the min."
tags: ["OI","DP","Brute Force","Divide and Conquer","Matrix Exponentiation","CDQ Divide and Conquer"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "集训队互测 2012 in the title rendered as “National Training Team Mutual Test 2012”, following the approved edition of post 1404; 「李超」 is the problem setter's name, romanized as [Li Chao]."
  - "The section headings A - Attack, B - Contra and C - Bomb were already English and are kept as is."
---

## A - Attack

On JZOJ this problem has a 10s time limit, so it got AC.

```cpp
// A.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 60100;

struct point
{
    int x, y, prop, id;
    bool operator<(const point &pt) const { return prop < pt.prop; }
} nodes[MAX_N], *cur[MAX_N];

int n, m;
char opt[10];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d%d%d", &nodes[i].x, &nodes[i].y, &nodes[i].prop), nodes[i].id = i;
    sort(nodes + 1, nodes + 1 + n);
    for (int i = 1; i <= n; i++)
        cur[nodes[i].id] = &nodes[i];
    while (m--)
    {
        scanf("%s", opt + 1);
        int x, y, x_, y_, k;
        if (opt[1] == 'Q')
        {
            scanf("%d%d%d%d%d", &x, &y, &x_, &y_, &k);
            bool flag = false;
            for (int i = 1; i <= n; i++)
                if (nodes[i].x <= max(x, x_) && nodes[i].x >= min(x, x_) && nodes[i].y <= max(y, y_) && nodes[i].y >= min(y, y_))
                {
                    k--;
                    if (k == 0)
                    {
                        printf("%d\n", nodes[i].prop), flag = true;
                        break;
                    }
                }
            if (!flag)
                puts("It doesn't exist.");
        }
        else
        {
            scanf("%d%d", &x, &y), x++, y++;
            swap(cur[x]->x, cur[y]->x), swap(cur[x]->y, cur[y]->y);
            swap(cur[x]->id, cur[y]->id), swap(cur[x], cur[y]);
        }
    }
    return 0;
}
```

<!--more-->

## B - Contra

This is a really interesting probability DP problem: you first binary search for a probability $p$, then use expectation DP plus matrix exponentiation to optimize it.

Let's first consider how to write the ordinary DP. Consider a state $g[i][j][k]$ representing the probability of reaching game $i$ with $j$ lives left and $k$ consecutive games in this kind of situation; the transition is very easy to come up with:

$$
\begin{cases} g[i + 1][min(j + 1, Q)][min(k + 1, R)] += g[i][j][k] \times p \\ g[i + 1][j - 1][0] += g[i][j][k] \times (1 - p) \end{cases}
$$

However, here $g$ is only a probability, and what we want is the expectation: we can see that it is enough to directly add each state's “**individual** contribution” to the answer into $answer$, that is:

$$
answer += g[i][j][k] \times k
$$

This gets you 50 points. The remaining 50 points need the very powerful matrix exponentiation. We notice that in the transition every state depends only on $i-1$, so matrix exponentiation can be used; also, there are very few combinations of $j, k$, so we can just brute-force compress the $j, k$ states into an array $mapper[j][k] = zippedCode$, and in the end it turns out there are no more than $100$ of them, so an $O(n^3)$ matrix multiplication passes directly.

Now consider three matrices: $f$ is the matrix needed for the final answer, $a_1$ is the probability matrix, and $a_2$ is the contribution matrix; then obviously $a_1 * a_2$ is the expectation matrix.

A quick recap of matrix multiplication:

$$
matrix[i][j] = \sum_{k = 1}^m{matrix_{size}} f[i][k] * g[k][j]
$$

Notice that this looks a lot like the form of a transition: row $i$ represents the original $S_i$ (state), column $j$ represents the $S_j$ (state) we are now transitioning to, so the element in row $i$, column $j$ of the matrix represents the probability of going from $S_i$ to $S_j$. Multiplying these probabilities repeatedly gives the pairwise probabilities between all states at round $n$. See the code for the exact matrix setup; it's easy to understand.

Our initial state is $S = 1$ and the final state is $S = stat_tot$, that is, the last state. So when setting up $f$ we just set the direct probability between the first two states to $1$. Then a $\log n$ fast exponentiation and we're done.

```cpp
// B.cpp
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int MAX_Q = 6, MAX_R = 21;
const double eps = 1e-8;

int N, R, Q, mat_siz, stat_siz, mapper[MAX_Q][MAX_R];
ll S;

struct matrix
{
    double mat[155][155];

    void clear() { memset(mat, 0, sizeof(mat)); }
    matrix operator*(const matrix &target) const
    {
        matrix ans;
        ans.clear();
        for (int i = 1; i <= mat_siz; i++)
            for (int j = 1; j <= mat_siz; j++)
                for (int k = 1; k <= mat_siz; k++)
                    ans.mat[i][j] += mat[i][k] * target.mat[k][j];
        return ans;
    }
} f, a1, a2, b;

double DP(double p)
{
    f.clear(), a1.clear(), a1.clear();
    for (int lives = 1; lives <= Q; lives++)
        for (int conseq = 0; conseq <= R; conseq++)
            if (lives >= conseq || lives == Q)
            {
                // this is how the probability matrix is set.
                a1.mat[mapper[lives][conseq]][mapper[min(lives + 1, Q)][min(conseq + 1, R)]] = p;
                a1.mat[mapper[lives][conseq]][mapper[lives - 1][0]] = 1.0 - p;
            }
    a1.mat[stat_siz][stat_siz] = 1;
    // this is how the Expectation works.
    for (int lives = 0; lives <= Q; lives++)
        for (int conseq = 0; conseq <= R; conseq++)
            if (lives >= conseq || lives == Q)
                a2.mat[mapper[lives][conseq]][stat_siz] = conseq;
    for (int i = 1; i <= mat_siz; i++)
        a2.mat[i][i] = 1;
    b = a1 * a2;
    f.mat[1][mapper[Q][0]] = 1;
    int tim = N;
    while (tim)
    {
        if (tim & 1)
            f = f * b;
        b = b * b;
        tim >>= 1;
    }
    return f.mat[1][stat_siz];
}

int main()
{
    scanf("%d%d%d%lld", &N, &R, &Q, &S);
    // zip the status up!
    for (int lives = 0; lives <= Q; lives++)
        for (int conseq = 0; conseq <= R; conseq++)
            if (lives >= conseq || lives == Q)
                mapper[lives][conseq] = ++stat_siz;
    // as the end status;
    ++stat_siz, mat_siz = stat_siz;
    if (DP(1.0) <= S)
        puts("Impossible.");
    else
    {
        double l = 0, r = 1.0;
        while (l < r - eps)
        {
            double mid = (l + r) / 2;
            if (DP(mid) > S)
                r = mid;
            else
                l = mid;
        }
        printf("%.6lf", l);
    }
    return 0;
}
```

## C - Bomb

This is also a good problem in terms of thinking.

### Finding the maximum contribution

We first find the maximum sum of distances between the three points. We find that the final contribution of three points $(x_1, y_1), (x_2, y_2), (x_3, y_3)$ to the answer is **of the form (looks like)**:

$$
\frac{answer}{2} = \begin{cases} x_1 + y_1 - x_2 - y_3 \\ x_1 + y_1 - x_3 - y_3 \end{cases}
$$

So we only need to maintain the maximum and minimum $x, y$, and the maximum and minimum $x + y, x - y$, and that's it.

### Finding the minimum contribution

This one is relatively a lot harder. We notice that the data range of this problem probably lets $O(n \log n)$ or $O(n)$ pass, and the time limit is 4s. So we can consider an $O(n \log n)$ approach. Consider sorting the whole sequence of points with $x$ as the key, then bringing in CDQ divide and conquer. Here CDQ divide and conquer has one advantage: when the interval is short, we just brute-force it in $O(n^3)$, which is fairly easy to write; once the interval gets long, when we filter the point set again to brute-force it, since the short intervals have already contributed to the answer, after sorting the small intervals by $y$ the number of points that could possibly form the answer is very small, so it also fits the overall complexity. See the code for details.

### Code

```cpp
// C.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 101000;

int n, ans = 0x3f3f3f3f;

struct point
{
    int x, y;
    bool operator<(const point &pt) const { return y < pt.y || (y == pt.y && x < pt.x); }
} pts[MAX_N], tmp[MAX_N];

bool compareByY(const point &pt1, const point &pt2) { return pt1.x < pt2.x || (pt1.x == pt2.x && pt1.y < pt2.y); }
int get(point A, point B, point C) { return max(A.x, max(B.x, C.x)) - min(A.x, min(B.x, C.x)) + max(A.y, max(B.y, C.y)) - min(A.y, min(B.y, C.y)); }

void solve(int l, int r)
{
    if (r - l < 15)
    {
        // brute force;
        for (int i = l; i <= r; i++)
            for (int j = i + 1; j <= r; j++)
                for (int k = j + 1; k <= r; k++)
                    ans = min(ans, get(pts[i], pts[j], pts[k]));
        return;
    }
    int mid = (l + r) >> 1, tot = 0;
    solve(l, mid), solve(mid, r);
    for (int i = mid; i <= r; i++)
        if (ans > abs(pts[i].x - pts[mid].x))
            tmp[++tot] = pts[i];
        else
            break;
    for (int i = mid - 1; i >= l; i--)
        if (ans > abs(pts[i].x - pts[mid].x))
            tmp[++tot] = pts[i];
        else
            break;
    sort(tmp + 1, tmp + 1 + tot);
    for (int rig = 3, lft = 1; rig <= tot; rig++)
    {
        while (abs(tmp[rig].y - tmp[lft].y) >= ans)
            lft++;
        for (int i = lft; i < rig; i++)
            for (int j = i + 1; j < rig; j++)
                ans = min(ans, get(tmp[rig], tmp[i], tmp[j]));
    }
}

int main()
{
    scanf("%d", &n);
    // largest module;
    int xmin, ymin, xmax, ymax, xpymax, xpymin, xmymax, xmymin;
    xmin = ymin = xpymin = xmymin = 1 << 30;
    xmax = ymax = xpymax = xmymax = -(1 << 30);
    for (int i = 1, x, y; i <= n; i++)
    {
        scanf("%d%d", &x, &y), swap(x, y);
        pts[i].x = x, pts[i].y = y;
        xmin = min(xmin, x), xmax = max(xmax, x);
        ymax = max(ymax, y), ymin = min(ymin, y);
        xpymax = max(xpymax, x + y), xpymin = min(xpymin, x + y);
        xmymax = max(xmymax, (x - y)), xmymin = min(xmymin, (x - y));
    }
    printf("%d\n", 2 * max(max(xpymax - xmin - ymin, xmax + ymax - xpymin), max(xmymax - xmin + ymax, xmax - xmymin - ymin)));
    // using the CDQ Division And Conquerence;
    sort(pts + 1, pts + 1 + n, compareByY);
    solve(1, n);
    printf("%d", 2 * ans);
    return 0;
}
```
