---
id: 2660
lang: en
translation_of: index.md
source_sha256: 4796841bb3b0fd544b489e98c3a6bb54950fbbce9231b3193350e27575f42007
title: "AtCoder Grand Contest 033 - Problem-Solving Report"
description: "Solutions to AGC 033 A to D: a BFS, a greedy boundary check, a Bash game on the tree diameter, and a DP with swapped state plus binary search."
tags: ["OI","Binary Search","DP","Game Theory","AtCoder"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "背包中大体积小价值的思想 rendered literally as “the knapsack idea for large volume and small value” (swapping the state and the stored value)."
  - "亦而反之 rendered as “and vice versa”, as written."
---

## A - Darker and Darker

A <span class="slang" data-zh="一眼题" data-gloss="yīyǎn tí, “one-glance problem”. A problem whose solution is obvious the moment you read it.">one-glance problem</span>: just run a BFS to find the longest shortest path.

<!--more-->

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

using namespace std;

const int MAX_N = 2020, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};

typedef pair<int, int> pii;

int n, m, dist[MAX_N][MAX_N];
char S[MAX_N][MAX_N];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%s", S[i] + 1);
    memset(dist, 0x3f, sizeof(dist));
    queue<pii> q;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (S[i][j] == '#')
                dist[i][j] = 0, q.push(make_pair(i, j));
    while (!q.empty())
    {
        int x = q.front().first, y = q.front().second;
        q.pop();
        for (int d = 0; d < 4; d++)
        {
            int dstx = x + dx[d], dsty = y + dy[d];
            if (S[dstx][dsty] != '\0' && dist[dstx][dsty] > dist[x][y] + 1)
                dist[dstx][dsty] = dist[x][y] + 1, q.push(make_pair(dstx, dsty));
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            ans = max(ans, dist[i][j]);
    printf("%d\n", ans);
    return 0;
}
```

## B - LRUD Game

Just greedily check, for each of the four directions, whether it can go out of bounds. Really <span class="slang" data-zh="傻逼" data-gloss="shǎbī. A vulgar insult, roughly “idiot” or “stupid”.">idiotic</span>.

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

using namespace std;

const int MAX_N = 2e5 + 200;

int n, m, len, sx, sy;
char S[MAX_N], T[MAX_N];

int main()
{
    scanf("%d%d%d%d%d%s%s", &n, &m, &len, &sx, &sy, S + 1, T + 1);
    // upper;
    bool ans = false;
    int x = sx, y = sy;
    for (int i = 1; i <= len; i++)
    {
        if (S[i] == 'U')
            x--;
        if (x < 1)
            ans = true;
        if (T[i] == 'D' && x < n)
            x++;
        if (x < 1)
            ans = true;
    }
    x = sx, y = sy;
    for (int i = 1; i <= len; i++)
    {
        if (S[i] == 'D')
            x++;
        if (x > n)
            ans = true;
        if (T[i] == 'U' && x > 1)
            x--;
        if (x > n)
            ans = true;
    }
    x = sx, y = sy;
    for (int i = 1; i <= len; i++)
    {
        if (S[i] == 'L')
            y--;
        if (y < 1)
            ans = true;
        if (T[i] == 'R' && y < m)
            y++;
        if (y < 1)
            ans = true;
    }
    x = sx, y = sy;
    for (int i = 1; i <= len; i++)
    {
        if (S[i] == 'R')
            y++;
        if (y > m)
            ans = true;
        if (T[i] == 'L' && y > 1)
            y--;
        if (y > m)
            ans = true;
    }
    puts(ans ? "NO" : "YES");
    return 0;
}
```

## C - Removing Coins

First consider the case of a chain: each move can be made on an end or on a non-endpoint, so it becomes a Bash game. Then, thinking carefully, whatever you do it ends up as a sub-chain of the diameter, so we decide by the diameter length $+ 1 \bmod 3$: if the remainder is $2$, the second player wins, and vice versa.

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

using namespace std;

const int MAX_N = 2e5 + 200;

int n, head[MAX_N], current, up[MAX_N];
bool tag[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 max_dist, pt;

void dfs(int u, int fa, int dep)
{
    if (max_dist < dep)
        pt = u, max_dist = dep;
    up[u] = fa;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
            dfs(edges[i].to, u, dep + 1);
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d", &n);
    for (int i = 1, u, v; i <= n - 1; i++)
        scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
    int A, B;
    dfs(1, 0, 0), max_dist = 0, A = pt, dfs(pt, 0, 0), B = pt;
    if ((max_dist + 1) % 3 == 2)
        puts("Second");
    else
        puts("First");
    return 0;
}
```

## D - Complexity

Jizhong once set a harder version of this problem, and of course I couldn't do it (the harder version seems to be done with a monotonic stack + segment tree).

First, come up with a <span class="slang" data-zh="sb" data-gloss="Initials of 傻逼 (shǎbī). A vulgar insult, roughly “idiot” or “stupid”.">stupid</span> $\Theta(n^5)$ brute force: let the state $dp[x][y][a][b]$ be the current submatrix, then just cut it up any which way.

Then we can optimize the state using the knapsack idea for large volume and small value. Clearly the answer has an upper bound of $\Theta(\log_2 n + \log_2 m)$, so we can let $dp[i][l][r][k]$ be the maximum distance we can extend to the right with top-left corner $(i, l)$, bottom-left corner $(i, r)$, and a cost of $k$.

This can be done in $\Theta(n^4 \log_2 n)$. Optimizing further, we find that for vertical cuts, looping about $\log_2 n$ times is actually good enough, so this part is $n^3 \log_2 n$. Horizontal cuts aren't so simple. Of course, for a horizontal cut we will definitely try to make the top and bottom parts as equal as possible, which is optimal, so we just wrap a binary search around it. Finally, enumerate the answer, then cut away like crazy.

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

using namespace std;

const int MAX_N = 220, MAX_LOG = 20;

int n, m, sum[MAX_N][MAX_N], dp[2][MAX_N][MAX_N][MAX_N];
char str[MAX_N][MAX_N];

bool check(int i, int j, int l, int r)
{
    int acc = sum[j][r] - sum[i - 1][r] - sum[j][l - 1] + sum[i - 1][l - 1];
    return acc == 0 || acc == 1LL * (j - i + 1) * (r - l + 1);
}

int calc(int p, int i, int j, int k)
{
    int l = i, r = j - 1, res = 0;
    while (l <= r)
    {
        int mid = (l + r) >> 1;
        res = max(res, min(dp[p][i][mid][k], dp[p][mid + 1][j][k]));
        if (dp[p][i][mid][k] < dp[p][mid + 1][j][k])
            r = mid - 1;
        else
            l = mid + 1;
    }
    return res;
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%s", str[i] + 1);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (str[i][j] == '#');
    if (check(1, n, 1, m))
        puts("0"), exit(0);
    for (int i = 1; i <= n; i++)
        for (int j = i; j <= n; j++)
            for (int k = 1, acc; k <= m; k = acc)
            {
                acc = k;
                while (acc <= m && check(i, j, k, acc))
                    acc++;
                for (int p = k; p < acc; p++)
                    dp[0][i][j][p] = acc - 1;
                if (acc == k)
                    dp[0][i][j][k] = k - 1, acc++;
            }
    for (int x = 1;; x++)
    {
        int c = x & 1, pc = !(x & 1);
        for (int i = 1; i <= n; i++)
            for (int j = i; j <= n; j++)
                for (int k = 1; k <= m; k++)
                {
                    int ret = dp[pc][i][j][k];
                    if (ret == m)
                        dp[c][i][j][k] = m;
                    else
                        dp[c][i][j][k] = max(dp[pc][i][j][ret + 1], calc(pc, i, j, k));
                }
        if (dp[c][1][n][1] == m)
            printf("%d\n", x), exit(0);
    }
    return 0;
}
```
