QOJ.ac

QOJ

Límite de tiempo: 4 s Límite de memoria: 512 MB Puntuación total: 100

#179. 在二叉树上放置奖牌

Estadísticas

你画了一张完美二叉树的图,如图 G.1 所示。图中展示的是一棵有限的树,但如果需要,你可以在叶子节点下方添加更多节点,使树变得任意深。

图 G.1. 完美二叉树图

树的节点与它们的深度相关联,深度定义如下:根节点的深度为 0,深度为 $d$ 的节点的子节点深度为 $d+1$。

你还有一堆奖牌,每枚奖牌上都刻有一个数字。你想知道这些奖牌是否可以放置在树状图上,并满足以下条件:

  • 刻有 $d$ 的奖牌必须放置在深度为 $d$ 的节点上。
  • 一个树节点最多只能容纳一枚奖牌。
  • 从放置了奖牌的节点到根节点的路径上,不能经过其他放置了奖牌的节点。

你必须按照奖牌在堆中从上到下的顺序,依次尝试放置奖牌。如果不存在满足条件的放置方式,你必须丢弃该奖牌,直接处理下一枚奖牌。

你可能有多种放置奖牌的选择。你希望找到最佳的放置方案。当存在两种或多种满足规则的放置方案时,能够放置更多靠上位置奖牌的方案更好。例如,当有四枚奖牌的两种放置方案时,一种放置了第 1 枚和第 2 枚奖牌,另一种放置了第 1 枚、第 3 枚和第 4 枚奖牌,前者更好。

在样例 1 中,你有一堆六枚奖牌,从上到下刻有的数字依次为 2, 3, 1, 1, 4, 2。

  • 第一枚刻有 2 的奖牌可以放置,如图 G.2 (A) 所示。
  • 然后第二枚刻有 3 的奖牌可以放置,如图 G.2 (B) 所示。
  • 第三枚刻有 1 的奖牌,如果按照上述方式放置第二枚奖牌,则无法放置,因为深度为 1 的两个节点都在已放置奖牌的节点到根节点的路径上。然而,通过替换已放置的第二枚奖牌以满足放置条件,可以实现如图 G.2 (C) 所示的放置。
  • 第四枚同样刻有 1 的奖牌,无论如何替换已放置的三枚奖牌,都无法满足条件进行放置。因此,这枚奖牌被丢弃。
  • 第五枚刻有 4 的奖牌可以放置,如图 G.2 (D) 所示。
  • 最后一枚刻有 2 的奖牌,无论如何替换,都无法放置在任何节点上。

图 G.2. 奖牌放置

输入格式

输入包含单个测试用例,格式如下:

$n$ $x_1$ $\vdots$ $x_n$

第一行包含一个整数 $n$,表示奖牌的数量 ($1 \le n \le 5 \times 10^5$)。接下来的 $n$ 行表示刻在奖牌上的正整数。第 $i$ 行包含一个整数 $x_i$ ($1 \le x_i \le 10^9$),表示从上往下数第 $i$ 枚奖牌上刻的数字。

输出格式

当选定最佳放置方案后,对于从 1 到 $n$ 的每个 $i$,如果第 $i$ 枚奖牌被放置,则输出 Yes,否则输出 No,每行输出一个结果。

样例

样例输入 1

6
2
3
1
1
4
2

样例输出 1

Yes
Yes
Yes
No
Yes
No

样例输入 2

10
4
4
4
4
4
4
4
4
1
4

样例输出 2

Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No

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.