---
id: 2150
lang: en
translation_of: index.md
source_sha256: 65cd41efa1ce346b8b0fb0169da670e32570b6cb19c0f66026b8439ba3dd6cf6
title: "Codeforces Round #500 (Div. 1) [based on EJOI] - Problem-Solving Report"
description: "Fairly obviously, we need to pick two point sets, and the contribution is the product of their ranges. Sort, then go through every interval of length n."
tags: ["OI","Codeforces"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "「based on EJOI」 in the title rendered as [based on EJOI], following the convention for contest/source tags."
  - "就不挂了 uses 挂 in the sense of posting code, not the glossary's failing sense, so the phrase 不挂了 got its own chip."
---

## A - Photo of The Sky

Fairly obviously, we need to pick two point sets, and the final contribution is the product of the two sets’ ranges. Sort them, and then just go through every interval of length $n$.

<!--more-->

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

using namespace std;

const int MAX_N = 1e5 + 200;

int n, ai[MAX_N << 1];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= (n << 1); i++)
        scanf("%d", &ai[i]);
    sort(ai + 1, ai + 1 + (n << 1));
    long long ans = 1e18;
    for (int i = 1; i <= n; i++)
        ans = min(ans, 1LL * (ai[i + n - 1] - ai[i]) * (ai[n << 1] - (i == 1 ? ai[n + 1] : ai[1])));
    printf("%lld\n", ans);
    return 0;
}
```

## B - Chemical table

<span class="slang" data-zh="妈的" data-gloss="mā de, “mom’s”. A common vulgar curse, roughly “damn it” or “f*ck”.">Damn it</span>, I thought about it forever and in the end found out it’s done with a bipartite graph. Drives me crazy.

Consider connecting each row number to its column number, which draws a bipartite graph. Think about what a triple formed by two edges means: say two left vertices $u, v$ and one right vertex $w$. We find that the right vertex lets the right vertices connected to $u, v$ be “shared”, because for the points $u, v$ of one row, the column information of $u, v$ (think of it as a bitset) can be merged, so a connected component of the bipartite graph makes the whole component work.

The answer is just the number of connected components minus a $1$.

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

using namespace std;

const int MAX_N = 4e5 + 200;

typedef pair<int, int> pii;

int n, m, q, sumx[MAX_N], sumy[MAX_N], color[MAX_N];
pii pts[MAX_N];
vector<int> G[MAX_N];

void dfs(int u, int org)
{
    color[u] = org;
    for (auto v : G[u])
        if (color[v] == 0)
            dfs(v, org);
}

int main()
{
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1, u, v; i <= q; i++)
        scanf("%d%d", &u, &v), G[u].push_back(v + n), G[v + n].push_back(u);
    int ans = 0;
    for (int i = 1; i <= n + m; i++)
        if (color[i] == 0)
            dfs(i, ++ans);
    printf("%d\n", ans - 1);
    return 0;
}
```

## C - Hills

A <span class="slang" data-zh="SB" data-gloss="Initials of 傻逼 (shǎbī). A vulgar insult, roughly “idiot” or “stupid”.">stupid</span> DP, harder than the previous problem (<span class="slang" data-zh="汗" data-gloss="hàn, “sweat”. Internet shorthand for a sweat-drop emoticon, showing embarrassment or awkward exasperation.">sweat</span>).

Think about designing the states, and you find you can just <span class="slang" data-zh="艹过去" data-gloss="cào guòqù, “f*ck through”: 艹 stands in for the vulgar 操 (cào). Crudely forcing a solution through until it gets accepted.">f*ck your way through it</span> directly in $\Theta(n^2)$. Three states turn out to be enough: placed, not placed, and already lowered. Then just transition however you like.

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

using namespace std;

const int MAX_N = 5050;

int n, ai[MAX_N], dp[MAX_N][MAX_N][3];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]);
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0][0] = 0;
    for (int i = 1; i <= n; i++)
    {
        dp[i][0][0] = 0;
        for (int j = 1; j <= (i + 1) >> 1; j++)
        {
            if (ai[i] > ai[i - 1])
                dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][0]);
            if (i != 1)
            {
                if (ai[i] > ai[i - 2] - 1)
                    dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][2]);
                else
                    dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][2] + ai[i - 2] - 1 - ai[i] + 1);
            }
            if (ai[i] <= ai[i - 1])
                dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][0] + ai[i - 1] - ai[i] + 1);
            // choose;
            dp[i][j][0] = min(dp[i][j][0], min(dp[i - 1][j][2], dp[i - 1][j][0]));
            if (ai[i] < ai[i - 1])
                dp[i][j][0] = min(dp[i][j][0], dp[i - 1][j][1]);
            if (ai[i] >= ai[i - 1])
                dp[i][j][2] = min(dp[i][j][2], min(dp[i - 1][j][1], dp[i - 1][j][0]) + ai[i] - ai[i - 1] + 1);
        }
    }
    for (int i = 1; i <= (n + 1) >> 1; i++)
        printf("%d ", min(dp[n][i][0], min(dp[n][i][1], dp[n][i][2])));
    puts("");
    return 0;
}
```

## D - AB-Strings

First, consecutive characters can be compressed; you can use a linked list. Once you have the linked list, you find that if the heads differ, you swap an odd number of units; if they are the same, just pick an odd and an even number.

However, this isn’t necessarily globally optimal. If you want to reduce things right now, this is obviously optimal; but global optimality requires that each operation makes the two strings as close to equal in size as possible, so that the next step is a better one.

The code didn’t pass, and it’s hard to write, so I <span class="slang" data-zh="不挂了" data-gloss="bù guà le, “won’t hang it up”. Here 挂 means putting something up on a page, i.e. posting the code.">won’t post it</span>.
