F. Folklore

Codeforces
IDCF10291F
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
Bob is a huge fan of Swift—the programming language developed by Apple for creating iOS apps, of course! Bob even has a special playlist of songs which he only plays when he is coding in Swift. Bob has $N$ songs on his music playlist, each with distinct song names. Currently, they are being played in some order, with the positions numbered $1$, $2$, $3$, $\\dots$, $N$. When Bob hits the shuffle button, he expects the order of the songs to be rearranged in a "sufficiently random" manner. To quantify this, Bob chooses some positive integer $K$, and expects each song to be _at least $K$ places away_ from its original position. Formally, a reordering of the songs is called sufficiently random if and only if the following holds. For each song, let $x$ be its position in the original ordering, and $y$ be its position in the reordering. Then, $| x -y | >= K$ must hold. Given Bob's song list and the integer $K$, output the songs in a different order which he considers sufficiently random, or tell Bob that this is impossible. If there are multiple solutions, output any of them. The first line of input contains two space-separated integers $N$ and $K$. The second line of input contains the $N$ songs in Bob's playlist in their original order, separated by spaces. Each song name is a single word consisting of no more than 10 upper and lower case English letters. If the task is impossible, output a single line containing the word _NO_. Otherwise, output a line containing the word _YES_, then output another line containing the same $N$ songs from the input, but in a sufficiently random order. $1 <= K <= N$ $2 <= N <= 10^5$ *Subtask 1* (45 points): $N <= 8$ *Subtask 2* (15 points): $K = 1$ *Subtask 3* (40 points): No further constraints. In the first sample test case, ## Input The first line of input contains two space-separated integers $N$ and $K$.The second line of input contains the $N$ songs in Bob's playlist in their original order, separated by spaces. Each song name is a single word consisting of no more than 10 upper and lower case English letters. ## Output If the task is impossible, output a single line containing the word _NO_. Otherwise, output a line containing the word _YES_, then output another line containing the same $N$ songs from the input, but in a sufficiently random order. [samples] ## Scoring $1 <= K <= N$$2 <= N <= 10^5$*Subtask 1* (45 points): $N <= 8$*Subtask 2* (15 points):$K = 1$*Subtask 3* (40 points):No further constraints. ## Note In the first sample test case, _august_ moves from position $1$ to position $4$, and $| 1 -4 | = 3$. _cardigan_ moves from position $2$ to position $3$, and $| 2 -3 | = 1$. _exile_ moves from position $3$ to position $2$, and $| 3 -2 | = 1$. _ricochet_ moves from position $4$ to position $1$, and $| 4 -1 | = 3$.
**Definitions** Let $ N \in \mathbb{Z}^+ $ be the number of songs. Let $ K \in \mathbb{Z}^+ $ satisfy $ 1 \leq K \leq N $. Let $ P = (p_1, p_2, \dots, p_N) $ be the original sequence of song names, where $ p_i $ is the song at position $ i $. A permutation $ Q = (q_1, q_2, \dots, q_N) $ of $ P $ is **sufficiently random** if for every $ i \in \{1, \dots, N\} $, if $ q_j = p_i $, then $ |i - j| \geq K $. **Constraints** 1. $ 2 \leq N \leq 10^5 $ 2. $ 1 \leq K \leq N $ **Objective** Find a permutation $ Q $ of $ P $ such that for every song originally at position $ i $, its new position $ j $ satisfies $ |i - j| \geq K $. If no such permutation exists, output "NO". Otherwise, output "YES" followed by $ Q $.
API Response (JSON)
{
  "problem": {
    "name": "F. Folklore",
    "description": {
      "content": "Bob is a huge fan of Swift—the programming language developed by Apple for creating iOS apps, of course! Bob even has a special playlist of songs which he only plays when he is coding in Swift. Bob h",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10291F"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Bob is a huge fan of Swift—the programming language developed by Apple for creating iOS apps, of course! Bob even has a special playlist of songs which he only plays when he is coding in Swift.\n\nBob h...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ N \\in \\mathbb{Z}^+ $ be the number of songs.  \nLet $ K \\in \\mathbb{Z}^+ $ satisfy $ 1 \\leq K \\leq N $.  \nLet $ P = (p_1, p_2, \\dots, p_N) $ be the original sequence of song nam...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments