QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 512 MB 満点: 100

#1337. 奇偶排序

統計

你有一个长度为 $n$ 的排列 $P$。在这个问题中,排列的元素是 $0$ 到 $n-1$ 之间的整数。 你的任务是执行最多 $30$ 次以下操作,将 $P$ 升序排序。

该操作由两个参数定义:一个表示操作模式的整数 $t$ ($0 \le t \le 1$) 和一个长度为 $n$ 的由 $0$ 和 $1$ 组成的字符串 $S$。

在过程开始时,我们有两个空序列 $A$ 和 $B$。 接下来,对于每个从 $1$ 到 $n$ 的 $i$,我们重复以下步骤: 如果 $S_i = 0$,则什么都不做。 如果 $S_i = 1$:如果 $P_i$ 是偶数,将 $P_i$ 添加到序列 $A$ 的末尾;否则,将 $P_i$ 添加到序列 $B$ 的末尾。

如果 $t = 0$,序列 $C$ 为序列 $A$ 和序列 $B$ 按该顺序的拼接。 如果 $t = 1$,序列 $C$ 为序列 $B$ 和序列 $A$ 按该顺序的拼接。

接下来,对于每个从 $1$ 到 $n$ 的 $i$,我们重复以下步骤: 如果 $S_i = 0$,则什么都不做。 如果 $S_i = 1$,用 $C$ 的第一个元素替换 $P_i$,并从 $C$ 中删除该元素。

例如,当 $n = 7$,$P = \{0, 4, 2, 3, 6, 5, 1\}$ 且我们选择 $t = 1$ 和 $S = 1101101$ 时,过程如下图所示。

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 15\,000$)。第二行包含 $n$ 个整数 $P_i$ ($0 \le P_i \le n - 1$,$P_i \neq P_j$ 当 $i \neq j$)。

输出格式

按以下格式打印一种可能的排序序列: 第一行打印一个整数 $k$:操作次数 ($0 \le k \le 30$)。 接下来的 $k$ 行中,第 $i$ 行描述第 $i$ 次操作,包含整数 $t_i$ ($0 \le t_i \le 1$) 和长度为 $n$ 的二进制字符串 $S$。

如果存在多个这样的序列,输出其中任意一个即可。注意,你不需要最小化 $k$。

样例

样例输入 1

7
0 4 2 3 6 5 1

样例输出 1

1
1 0100101

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.