---
id: 1899
lang: en
translation_of: index.md
source_sha256: 37263a2be753e8c99fe8cb4643282004666e7e2955454228d39243ad9c713cc9
title: "Codeforces Round #609 (Div. 1) - Problem-Solving Report"
description: "Take the first k digits as the repeating block, compare it with each following block of length k, and if the first block is smaller, just add one."
tags: ["OI","Codeforces","Median"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## A - Long Beautiful Integer

Take the first $k$ digits as the repeating block, and compare it against each following block of length $k$; if the first block is smaller, just add one to it.

<!--more-->

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

using namespace std;

const int MAX_N = 2e5 + 200;

int n, k;
char str[MAX_N];
int digits[MAX_N];

int main()
{
    scanf("%d%d%s", &n, &k, str + 1);
    for (int i = 1; i <= n; i++)
        digits[i] = str[i] - '0';
    // compare the nxt substr;
    bool flag = false;
    for (int i = k + 1; i <= n; i++)
        if (digits[(i - 1) % k + 1] > digits[i])
            break;
        else if (digits[(i - 1) % k + 1] < digits[i])
        {
            flag = true;
            break;
        }
    if (flag)
        digits[k]++;
    for (int i = k; i >= 1; i--)
        if (digits[i] >= 10)
            digits[i] -= 10, digits[i - 1]++;
    printf("%d\n", n);
    for (int i = 1; i <= n; i++)
        printf("%d", digits[(i - 1) % k + 1]);
    return 0;
}
```

## C - K Integers

This problem is pretty interesting. We consider that the final answer should come from first assembling the subsegment, then sorting it internally. The internal sorting can be handled by just maintaining the inversion count dynamically, so assembling the subsegment is the more troublesome part. Let us set the numbers in $[1, k]$ to $1$ and the rest to $0$. So now what we need to do is bring a bunch of $1$s together. We can enumerate the midpoint of the subsegment, move these $1$s toward that point, and then compute the moving cost. You can see this is a bit like <span class="slang" data-zh="货仓选址" data-gloss="huòcāng xuǎnzhǐ, “choosing a warehouse site”. A classic textbook problem: place a warehouse so the total distance to all stores is minimal.">Warehouse Location</span>; just find it with binary search.

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

using namespace std;

const int MAX_N = 2e5 + 200;

ll n, pi[MAX_N], pos[MAX_N], nodes[2][MAX_N], inv_f[MAX_N], ans[MAX_N];
bool vis[MAX_N];

inline int lowbit(int x) { return x & (-x); }

void update(int id, int x, ll d)
{
    for (; x <= n; x += lowbit(x))
        nodes[id][x] += d;
}

ll query(int id, int x, ll ret = 0)
{
    for (; x > 0; x -= lowbit(x))
        ret += nodes[id][x];
    return ret;
}

int main()
{
    scanf("%lld", &n);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &pi[i]), pos[pi[i]] = i;
    for (int i = 1; i <= n; i++)
    {
        inv_f[i] = inv_f[i - 1] + i - query(0, pos[i]) - 1;
        update(0, pos[i], 1);
    }
    memset(nodes[0], 0, sizeof(nodes[0]));
    ll LB = n, RB = 1;
    for (int i = 1; i <= n; i++)
        update(0, i, 1), update(1, i, i), vis[i] = true;
    for (int i = 1; i <= n; i++)
    {
        LB = min(LB, pos[i]), RB = max(RB, pos[i]);
        update(0, pos[i], -1), update(1, pos[i], -pos[i]), vis[pos[i]] = false;
        int l = LB, r = RB, res = LB;
        while (l <= r)
        {
            int mid = (l + r) >> 1;
            int l_key = query(0, mid - 1) - query(0, LB);
            int r_key = query(0, RB) - query(0, mid);
            l_key = mid - LB - l_key, r_key = RB - mid - r_key;
            if (l_key <= r_key)
                res = mid, l = mid + 1;
            else
                r = mid - 1;
        }
        ll k1 = query(1, res) - query(1, LB), k1_ = query(0, res) - query(0, LB);
        ll k2 = query(1, RB) - query(1, res), k2_ = query(0, RB) - query(0, res);
        ans[i] = (k1 - k1_ * LB - (k1_ * (k1_ - 1) / 2)) + (k2_ * RB - k2 - (k2_ * (k2_ - 1) / 2));
    }
    for (int i = 1; i <= n; i++)
        printf("%lld ", inv_f[i] + ans[i]);
    return 0;
}
```
