---
id: 2581
lang: en
translation_of: index.md
source_sha256: 0b5276aaa0f3132b6fd1f3e4de79f0cfcb57aac6f7cedf0eb0e4553bf4e3c6ca
title: "AtCoder Grand Contest 041 - Problem-Solving Report"
description: "Solutions to AGC 041 A to D: parity, binary search on the answer, a diagonal domino construction, and a knapsack-style DP over difference arrays."
tags: ["OI","Binary Search on the Answer","DP","Knapsack","Constructive"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Image alt 截屏2020-07-04 下午4.35.24 (the macOS screenshot file name) rendered as “Screenshot 2020-07-04 at 4.35.24 PM”."
  - "lcx kept as written; it is a handle or initials of a person not otherwise identified."
  - "单元数 (Domino Quality) rendered literally as “unit count”."
---

## A - Table Tennis Training

A <span class="slang" data-zh="思博" data-gloss="sībó, a homophone of SB, i.e. 傻逼 (shǎbī), “idiot”. Here: dead simple, dumb.">brain-dead</span> problem; just consider the two ends and the parity.

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

using namespace std;

typedef long long ll;

int main()
{
    ll n, A, B, ans = 0x7fffffffffffffff;
    scanf("%lld%lld%lld", &n, &A, &B);
    ans = min(A - 1, n - B) + 1 + ((B - A - 1) >> 1);
    if (!((A^B) & 1))
        ans = min(ans, (B - A) >> 1);
    printf("%lld\n", ans);
    return 0;
}
```

<!--more-->

## B - Voting Judges

First you need to realize that this problem can be binary searched, because if a larger answer is feasible, then a smaller answer is feasible too. Next, think about writing a check. First rule out the case where it can't make the cut even with the votes added to it alone, then count the votes that can be provided by the ones smaller than $a_{mid}$ among the $a_i$. Finally, just check whether this many votes can get it to make the cut.

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

using namespace std;

const int MAX_N = 1e5 + 200;

int n, m, V, P, ai[MAX_N];

bool check(int mid)
{
    if (ai[mid] + m < ai[P])
        return false;
    long long acc = 0;
    for (int i = P; i <= n; i++)
        if (i != mid && ai[i] < ai[mid] + m)
            acc += min(m, ai[mid] + m - ai[i]);
    return acc >= 1LL * (V - 1) * m;
}

int main()
{
    scanf("%d%d%d%d", &n, &m, &V, &P);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]);
    sort(ai + 1, ai + 1 + n), reverse(ai + 1, ai + 1 + n);
    V = max(1, V - P + 1);
    int l = P, r = n, res = P;
    while (l <= r)
    {
        int mid = (l + r) >> 1;
        if (check(mid))
            res = mid, l = mid + 1;
        else
            r = mid - 1;
    }
    printf("%d\n", res);
    return 0;
}
```

## C - Domino Quality

First, we can find that case for $3$ with a unit count of $2$, and that case can just be output directly; besides that, we can work out ourselves the cases $4 \to 7$ with a unit count of $3$. After that, if $n > 7$, write it as $n = 4x + y, y > 3$, then just piece them together along the diagonal.

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

using namespace std;

int n;
char buffer[1010][1010];

const char m3[3][4] =
    {"aa.",
     "..a",
     "..a"};

const char m4[4][5] =
    {"aabc",
     "ddbc",
     "bcaa",
     "bcdd"};

const char m5[5][6] =
    {"aabba",
     "bcc.a",
     "b..cb",
     "a..cb",
     "abbaa"};

const char m6[6][7] =
    {"aabc..",
     "ddbc..",
     "..aabc",
     "..ddbc",
     "bc..aa",
     "bc..dd"};

const char m7[7][8] =
    {"aabbcc.",
     "dd.dd.a",
     "..d..da",
     "..d..db",
     "dd.dd.b",
     "..d..dc",
     "..d..dc"};

void writeBuffer(int id, int x, int y)
{
    for (int i = 0; i < id; i++)
    {
        const char *str;
        switch (id)
        {
        case 3:
            str = m3[i];
            break;
        case 4:
            str = m4[i];
            break;
        case 5:
            str = m5[i];
            break;
        case 6:
            str = m6[i];
            break;
        case 7:
            str = m7[i];
            break;
        }
        for (int j = 0; str[j]; j++)
            buffer[x + i][y + j] = str[j];
    }
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            buffer[i][j] = '.';
    if (n <= 2)
        puts("-1"), exit(0);
    if (n <= 7)
        writeBuffer(n, 0, 0);
    else
    {
        int base = 0;
        writeBuffer(n % 4 + 4, base, base), base += n % 4 + 4;
        while (base < n)
            writeBuffer(4, base, base), base += 4;
    }
    for (int i = 0; i < n; i++)
        printf("%s\n", buffer[i]);
    return 0;
}
```

## D - Problem Scores

This should be the most interesting problem this time; it was posted a while ago in one of lcx's coach group chats.

Really, it just needs its $k + 1$ prefix sum to be strictly greater than its $k$ suffix sum. Written out, that is:

$$
\sum_{i = 1}^{k + 1} a_i > \sum_{i = n - k + 1}^n a_i
$$

There are $n$ such constraints in total, and we try to find properties to cut them down. First, for the case $k > \lfloor \frac{n}{2} \rfloor$, the prefix and the suffix overlap in the middle; this part can be moved to one side and canceled directly, turning it into the case $k \leq \lfloor \frac{n}{2} \rfloor$.

Next, consider using the property $a_i \leq a_{i + 1}$; we find that if it is satisfied when $k = \lfloor \frac{n}{2} \rfloor$, the cases $k \leq \lfloor \frac{n}{2} \rfloor$ are fully satisfied.

So now we only need to guarantee $\sum_{i = 1}^{k + 1} a_i > \sum_{i = n - k + 1}^n a_i, k = \lfloor \frac{n}{2} \rfloor$.

Now we take differences of this sequence, so that $a_k = 1 + \sum_{i = 1}^k \Delta_i$. First, obviously $\Delta \geq 0$. Second, when plugging back into the formula, we find that the prefix sums can be broken down into $\sum \Delta_i c_i$, where this $c_i$ is easy to compute. Let's substitute:

$$
\begin{gathered} \sum_{i = 1}^{k + 1} 1 + \sum_{j = 1}^i \Delta_j > \sum_{i = n - k + 1}^n 1 + \sum_{j = 1}^i \Delta_j \\ 1 + \sum_{i = 1}^{k + 1} \sum_{j = 1}^i \Delta_j > \sum_{i = n - k + 1}^n \sum_{j = 1}^i \Delta_j \\ \sum_{i = 1}^{k + 1} \sum_{j = 1}^i \Delta_j \geq \sum_{i = n - k + 1}^n \sum_{j = 1}^i \Delta_j \\ \sum_{j = 1}^n \Delta_j \sum_{i = 1}^{k + 1} [j \leq i] \geq \sum_{j = 1}^n \Delta_j \sum_{i = n - k + 1}^n [j \leq i] \\ \sum_{j = 1}^{k + 1} \Delta_j (k + 2 - j) \geq \sum_{j = 1}^n \Delta_j (n - \max(j, n - k + 1) + 1) \end{gathered}
$$

This gives:

$$
\begin{gathered} (k + 1) \Delta_1 + \sum_{j = 2}^{k + 1} \Delta_j (k + 2 - j) \geq k\Delta_1 + \sum_{j = 2}^n \Delta_j (n - \max(j, n - k + 1) + 1) \\ \Delta_1 \geq \sum_{j = 2}^n \Delta_j (n - \max(j, n - k + 1) + 1) - \sum_{j = 2}^{k + 1} \Delta_j (k + 2 - j) \\ \Delta_1 \geq \sum_{j = 2}^n \Delta_j (n - \max(j, n - k + 1) + 1) - \sum_{j = 2}^n \Delta_j \max(0, k + 2 - j) \\ \Delta_1 \geq \sum_{j = 2}^n \Delta_j (n + 1 - (\max(j, n - k + 1) + \max(0, k + 2 - j))) \\ \Delta_1 \geq \sum_{j = 2}^n \Delta_j (n + 1 - \max(j, k + 2, n - k + 1, n + 3 - j)) \\ \Delta_1 \geq \sum_{j = 2}^n \Delta_j (n + 1 + \min(-j, - k - 2, - n + k - 1, - n - 3 + j)) \\ \Delta_1 \geq \sum_{j = 2}^n \Delta_j \min(n - j + 1, n - k - 1, k, j - 2) \\ \end{gathered}
$$

As a reminder, $k = \lfloor \frac{n}{2} \rfloor$:

![Screenshot 2020-07-04 at 4.35.24 PM](media/截屏2020-07-04-下午4.35.24.png)

The intervals are discrete, so in the end $\min(n - j + 1, n - k - 1, k, j - 2) = \min(j - 2, n - j + 1)$. In the end, the whole inequality looks like:

$$
\Delta_1 \geq [\Delta_2, \Delta_3, \dots \Delta_n] \cdot [0, 1, 2, 3, \dots 3, 2, 1]
$$

Let:

$$
\begin{gathered} a = \Delta_2 + \Delta_3 + \Delta_4 + \dots + \Delta_n \\ b = [\Delta_2, \Delta_3, \dots \Delta_n] \cdot [0, 1, 2, 3, \dots 3, 2, 1] \end{gathered}
$$

We find that $\Delta_1 \leq n - 1 - a, \Delta_1 \geq b$, so $\Delta_1$ can be chosen in $\max(0, n - 1 - (a + b))$ ways. $a + b = [\Delta_2, \Delta_3, \dots \Delta_n] \cdot [1, 2, 3, 4, \dots 4, 3, 2]$, so we use $DP$ to count, for a sum of $j = a + b$, the number of $\Delta_i$ sequences, and then multiplying that count by $\max(0, n - 1 - (a + b))$ gives the answer.

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

using namespace std;

const int MAX_N = 5050;

int n, mod, dp[MAX_N], coeff[MAX_N];

int main()
{
    scanf("%d%d", &n, &mod), dp[0] = 1;
    for (int i = 1; i < n; i++)
        for (int j = min(n - i + 1, i), k = j; j < n; j++)
            dp[j] = (0LL + dp[j] + dp[j - k]) % mod;
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans = (0LL + ans + 1LL * dp[i] * (n - i) % mod) % mod;
    printf("%d\n", ans);
    return 0;
}
```
