QOJ.ac

QOJ

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

#2348. 双射

Estadísticas

考虑平面上从 $(0, 0)$ 到 $(n, n)$ 的路径,由向右(“R”)和向上(“U”)的单位步长组成。已知此类不同路径的数量为二项式系数:

$$\text{choose}(2n, n) = \frac{(2n)!}{n! \cdot n!}$$

例如,当 $n = 2$ 时,有六条这样的路径:“RRUU”、“RURU”、“RUUR”、“URRU”、“URUR”、“UURR”。

字符串 $U$ 是一个合法括号序列,如果它是空串,或者形式为“$(V)$”,或者是两个形式为“$VW$”的字符串的拼接,其中 $V$ 和 $W$ 是合法括号序列。考虑包含 $n$ 对括号的合法括号序列。已知此类不同序列的数量为卡特兰数,特别地,可以计算如下:

$$C_n = \frac{1}{n + 1} \cdot \text{choose}(2n, n)$$

例如,当 $n = 2$ 时,有两个这样的序列:“(())”、“()()”。

请构造一个反映这一事实的双射。更具体地说,给定一条包含 $n$ 步向右和 $n$ 步向上的路径,构造一个包含 $n$ 对括号的合法括号序列,并额外记录一个 $0$ 到 $n$(含)之间的整数 $k$。之后,给定该序列和整数 $k$,还原出原始路径。

交互

在本题中,你的程序在每个测试点上将被运行两次。

在第一次运行中,程序需要对路径进行编码。第一行包含单词“path”。第二行包含一个整数 $n$:路径长度的一半($1 \le n \le 300$)。第三行包含一条 $2n$ 步的路径:由 $n$ 个字母“R”和 $n$ 个字母“U”以某种顺序组成。

在第一行,输出任意一个包含 $n$ 个“(”字符和 $n$ 个“)”字符的合法括号序列。在第二行,输出任意一个整数 $k$($0 \le k \le n$)。

在第二次运行中,程序需要还原路径。第一行包含单词“brackets”。第二行包含一个整数 $n$,与第一次运行相同:括号序列长度的一半($1 \le n \le 300$)。第三行包含一个包含 $n$ 个“(”字符和 $n$ 个“)”字符的合法括号序列。第四行包含一个整数 $k$($0 \le k \le n$)。该序列和整数即为第一次运行中输出的内容。

在第一行,输出还原后的初始路径:$n$ 个字母“R”和 $n$ 个字母“U”,顺序与第一次运行输入中的路径相同。

在每次运行中,每一行输入(包括最后一行)均以换行符结尾。

样例

在每个测试点上,第二次运行的输入取决于第一次运行中程序的输出。

以下展示了某程序在第一个测试点上的两次运行:

标准输入 标准输出
path
2
RRUU
(())
0
brackets
2
(())
0
RRUU

以下展示了某程序在第二个测试点上的两次运行:

标准输入 标准输出
path
3
RUURRU
(())()
3
brackets
3
(())()
3
RUURRU

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.