QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 256 MB Puntuación total: 100 Interactivo

#1458. 二分查找算法

Estadísticas

这是一个交互式问题。

我有一个隐藏的排列 $p_1, p_2, \dots, p_n$。你不需要猜出它。你的任务是设计一个数据结构(这违反了规则!),支持对一个初始为空的集合 $S$ 进行以下操作:

  • “add $x$” —— 将元素 $x$ 加入 $S$ 中。
  • “delete $x$” —— 从 $S$ 中删除元素 $x$。
  • “getMin” —— 输出 $S$ 中使得 $p_x$ 最小的元素 $x$。

你需要在每次其他类型的操作后执行一次 “getMin”。

你不知道这个排列,但你可以进行查询。在一次查询中,你可以选择 $k$ 个不同的下标 $x_1, x_2, \dots, x_k$($k$ 为某个值),作为回报,我会告诉你这些下标的排列 $y_1, y_2, \dots, y_k$,使得 $p_{y_1} < p_{y_2} < \dots < p_{y_k}$。换句话说,我会根据 $p$ 对这些下标进行排序。

注意,所有的 $x_i$ 在查询时都必须存在于 $S$ 中。

在 1 次查询中执行 “getMin” 是很容易的 —— 只需对 $S$ 中的所有元素进行排序即可。使用若干次查询(查询中 $k$ 的总和为 $O(\log n)$)来执行它也不难。你能展示你的算法能力并同时满足这两个要求吗?

注意,由于你不知道 $p$,且我的任务是让你的解法失败,我可以根据你的查询改变 $p$,但前提是必须保证我之前所有的回答都是正确的。我也可以根据你的查询选择你需要执行的操作顺序。

输入格式

首先给定一个整数 $n$ ($1 \le n \le 8000$),表示元素的数量。每个元素会被插入并删除恰好一次。

交互

接下来会有恰好 $2n$ 轮交互。

每一轮交互包含 4 个阶段:

  1. 你读取下一行操作:要么是 “add $x$”,要么是 “delete $x$”,其中 $1 \le x \le n$;
  2. 你选择某个 $0 \le k \le \min(|S|, 30)$,并在单独的一行中打印 $k+1$ 个数字:先打印 $k$,然后打印 $x_1, x_2, \dots, x_k$:这是你想要排序的 $k$ 个元素。你选择的元素必须在 $1$ 到 $n$ 之间,必须互不相同,且此时必须存在于 $S$ 中。注意,$S$ 已经根据阶段 1 进行了更新;
  3. 你读取下一行中的 $k$ 个整数 $y_1, y_2, \dots, y_k$:$y$ 是你刚才打印的 $x$ 的一个排列,满足 $p_{y_1} < p_{y_2} < \dots < p_{y_k}$;
  4. 你在单独的一行中打印一个整数 $x$,使得 $x \in S$ 且 $p_x$ 尽可能小。如果 $S$ 为空,则打印 $-1$。

保证所有 $2n$ 个可能的操作(对于所有 $1 \le x \le n$ 的 “add $x$” 和 “delete $x$”)都会恰好发生一次,并且对于每个 $x$,操作 “add $x$” 总会先于 “delete $x$” 发生。

在读取任何内容之前,请务必打印换行符并刷新输出。

样例

样例输入 1

3
add 1
1
add 3
3 1
delete 1
3
add 2
3 2
delete 3
2
delete 2

样例输出 1

1 1
1
2 1 3
3
1 3
3
2 2 3
3
1 2
2
0
-1

说明

在样例中,$p = [2, 3, 1]$。

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.