← CSES Better

Rearrange Students into an Alternating Line

ZIO · 2022 · Zonal Informatics Olympiad · P1

Open official problem ↗
Ask AI

Open a hint-first prompt in a new AI chat. Grok may ask you to confirm before sending.

Open Claude Desktop

strings · greedy · rearrangement

The organizer’s problem statement is available at the official source link.

alternating sequence

Compare both alternating targets

Minimize moved positions while maximizing adjacent unlike genders.

B G B G ...G B G B ...min moves = N ... matches
  1. 1

    Build both targets

    Build both alternating patterns for the same length.
  2. 2

    Count fixed positions

    Count original positions that already match each pattern.
  3. 3

    Minimize moved students

    Subtract fixed positions from N and choose the lower move count.
Tools
1
2
3
4
5
6
7
8
9
10
11
12
Draft saved locally
|12 lines, 156 chars

Run output

Run your code to see output.