codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1149A.md (1939B)


      1 Source: https://codeforces.com/problemset/problem/1149/A
      2 
      3 A. Prefix Sum Primes
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 We're giving away nice huge bags containing number tiles! A bag we want to present to you contains $n$ tiles. Each of them has a single number written on it — either $1$ or $2$.
     22 
     23 However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get.
     24 
     25 Can you win the prize? Hurry up, the bags are waiting!
     26 
     27 Input
     28 
     29 The first line of the input contains a single integer $n$ ($1 \leq n \leq 200\,000$) — the number of number tiles in the bag. The following line contains $n$ space-separated integers $a_1, a_2, \dots, a_n$ ($a_i \in \{1, 2\}$) — the values written on the tiles.
     30 
     31 Output
     32 
     33 Output a permutation $b_1, b_2, \dots, b_n$ of the input sequence $(a_1, a_2, \dots, a_n)$ maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any.
     34 
     35 Examples
     36 
     37 Input
     38 
     39 ```
     40 5
     41 1 2 1 2 1
     42 ```
     43 
     44 Output
     45 
     46 ```
     47 1 1 1 2 2
     48 ```
     49 
     50 Input
     51 
     52 ```
     53 9
     54 1 1 2 1 1 1 2 1 1
     55 ```
     56 
     57 Output
     58 
     59 ```
     60 1 1 1 2 1 1 1 2 1
     61 ```
     62 
     63 Note
     64 
     65 The first solution produces the prefix sums $1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}}$ (four primes constructed), while the prefix sums in the second solution are $1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}}$ (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible.