QOJ.ac

QOJ

时间限制: 3 s 内存限制: 256 MB 总分: 100

#104. 最长美丽序列

统计

给定两个长度为 $n$ 的非负整数序列:$a_1, a_2, \dots, a_n$ 和 $k_1, k_2, \dots, k_n$。如果一个由 $m$ 个整数组成的序列 $i_1, i_2, \dots, i_m$ 满足以下条件,则称其为“优美序列”:

  • $1 \le i_1 < i_2 < \dots < i_m \le n$。换句话说,序列必须是递增的。
  • 对于所有 $1 < j \le m$,满足 $bitCount(a_{i_{j-1}} \text{ AND } a_{i_j}) = k_{i_j}$。

求最长的优美序列。

输入格式

第一行包含一个正整数 $n$ ($1 \le n \le 10^5$),表示序列 $a$ 和 $k$ 的长度。 第二行包含 $n$ 个非负整数 $a_i$ ($0 \le a_i < 2^{20}$),表示序列 $a$。 第三行包含 $n$ 个非负整数 $k_i$ ($0 \le k_i \le 20$),表示序列 $k$。 两个序列中的数字均由空格分隔。

输出格式

第一行输出一个整数 $m$,表示最长优美序列的长度。 第二行输出 $m$ 个整数,表示最长优美序列,数字间用空格分隔。如果存在多个解,输出其中任意一个即可。

子任务

本题包含四个子任务:

  1. $1 \le n \le 15, 0 \le a_i < 2^{20}$。该子任务分值为 7 分。
  2. $1 \le n \le 5000, 0 \le a_i < 2^{20}$。该子任务分值为 16 分。
  3. $1 \le n \le 10^5, 0 \le a_i < 2^8$。该子任务分值为 17 分。
  4. $1 \le n \le 10^5, 0 \le a_i < 2^{20}$。该子任务分值为 60 分。

每个子任务仅在通过所有前置子任务时才会被计分。

样例

输入 1

4
1 2 3 4
10 0 1 0

输出 1

4
1 2 3 4

输入 2

2
8 9
20 0

输出 2

1
1

输入 3

5
5 3 5 3 5
10 1 20 1 20

输出 3

2
1 2

说明

$bitCount(x)$ 表示 $x$ 二进制表示中 1 的个数,例如 $bitCount(5_{10}) = bitCount(101_2) = 2$,$bitCount(0) = 0$,$bitCount(8) = 1$。

AND 是二进制运算,它对两个等长二进制表示的每一位对应位执行逻辑与操作,例如 $11_{10} \text{ AND } 13_{10} = 1011_2 \text{ AND } 1101_2 = 1001_2 = 9$,$7_{10} \text{ AND } 16_{10} = 111_2 \text{ AND } 10000_2 = 0_2 = 0_{10}$。

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.