A12504 | Prefix Sum Primes
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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$ .
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.
Can you win the prize? Hurry up, the bags are waiting!
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.
Can you win the prize? Hurry up, the bags are waiting!
输入格式
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.
输出格式
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.
输入输出样例
输入 #1
5 1 2 1 2 1
输出 #1
1 1 1 2 2
输入 #2
9 1 1 2 1 1 1 2 1 1
输出 #2
1 1 1 2 1 1 1 2 1
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted