AT_arc221_d.Two Balanced Subtrees

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a positive integer NN.

There is a complete binary tree with 2N−12^N-1 vertices. The vertices are numbered 11 through 2N−12^N-1.

Vertex 11 is the root, and for each i (1≤i<2N−1)i\ (1\leq i\lt 2^{N-1}), vertex ii has vertices 2i2i and 2i+12i+1 as its children.

Find one way to write an integer between 11 and 2N−12^N-1, inclusive, on the vertices (where all 2N−12^N-1 written integers are distinct) such that the following condition is satisfied.

  • For each i (1≤i<2N−1)i\ (1\leq i\lt 2^{N-1}), the absolute difference between the sum of the integers written on the vertices of the subtree rooted at vertex 2i2i and the sum of the integers written on the vertices of the subtree rooted at vertex 2i+12i+1 is 11.

It can be proved that there always exists a way to write the integers satisfying the condition under the constraints of this problem.

给定一个正整数 NN。

存在一棵包含 2N−12^N-1 个顶点的满二叉树。这些顶点编号为 11 至 2N−12^N-1。

顶点 11 为根节点;对每个 i (1≤i<2N−1)i\ (1\leq i\lt 2^{N-1}),顶点 ii 的两个子节点分别为顶点 2i2i 和 2i+12i+1。

请给出一种方案:将 11 到 2N−12^N-1(含端点)之间的整数填入各顶点(所有 2N−12^N-1 个填入的整数互不相同),使得满足如下条件:

  • 对每个 i (1≤i<2N−1)i\ (1\leq i\lt 2^{N-1}),以顶点 2i2i 为根的子树中所有顶点上所填整数之和,与以顶点 2i+12i+1 为根的子树中所有顶点上所填整数之和的绝对差值为 11。

在本题约束下,可以证明总存在满足该条件的填数方案。

输入格式

The input is given from Standard Input in the following format:

NN

输入从标准输入中按以下格式给出:

NN

输出格式

Let PiP_i be the integer written on vertex ii. Output:

P1P_1 P2P_2 …\ldots P2N−1P_{2^N-1}

(P1,P2,…,P2N−1)(P_1,P_2,\ldots,P_{2^N-1}) must be a permutation of (1,2,…,2N−1)(1,2,\ldots,2^N-1).

If there are multiple valid ways to write the integers, any of them will be accepted.

设顶点 ii 上写的整数为 PiP_i。输出:

P1P_1 P2P_2 …\ldots P2N−1P_{2^N-1}

(P1,P2,…,P2N−1)(P_1,P_2,\ldots,P_{2^N-1}) 必须是 (1,2,…,2N−1)(1,2,\ldots,2^N-1) 的一个排列。

若存在多种合法的填数方式,输出任意一种即可。

输入输出样例

  • 输入#1

    3

    输出#1

    1 7 2 3 4 5 6

说明/提示

Sample 1 Explanation:
We can verify that this assignment satisfies the condition, as follows.

  • i=1i=1: The sum of the integers written on the vertices of the subtree rooted at vertex 22 is 1414, the sum of the integers written on the vertices of the subtree rooted at vertex 33 is 1313, and ∣14−13∣=1|14-13|=1.
  • i=2i=2: The sum of the integers written on the vertices of the subtree rooted at vertex 44 is 33, the sum of the integers written on the vertices of the subtree rooted at vertex 55 is 44, and ∣3−4∣=1|3-4|=1.
  • i=3i=3: The sum of the integers written on the vertices of the subtree rooted at vertex 66 is 55, the sum of the integers written on the vertices of the subtree rooted at vertex 77 is 66, and ∣5−6∣=1|5-6|=1.

Constraints

  • 2≤N≤182\leq N\leq 18
  • All input values are integers.

样例 1 解释:
我们可以验证该赋值满足题目条件,具体如下:

  • i=1i=1:以顶点 22 为根的子树中各顶点上所写整数之和为 1414,以顶点 33 为根的子树中各顶点上所写整数之和为 1313,且 ∣14−13∣=1|14-13|=1。
  • i=2i=2:以顶点 44 为根的子树中各顶点上所写整数之和为 33,以顶点 55 为根的子树中各顶点上所写整数之和为 44,且 ∣3−4∣=1|3-4|=1。
  • i=3i=3:以顶点 66 为根的子树中各顶点上所写整数之和为 55,以顶点 77 为根的子树中各顶点上所写整数之和为 66,且 ∣5−6∣=1|5-6|=1。

限制条件

  • 2≤N≤182\leq N\leq 18
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页