A13627 | Merging Towers
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You have a set of $n$ discs, the $i$ -th disc has radius $i$ . Initially, these discs are split among $m$ towers: each tower contains at least one disc, and the discs in each tower are sorted in descending order of their radii from bottom to top.
You would like to assemble one tower containing all of those discs. To do so, you may choose two different towers $i$ and $j$ (each containing at least one disc), take several (possibly all) top discs from the tower $i$ and put them on top of the tower $j$ in the same order, as long as the top disc of tower $j$ is bigger than each of the discs you move. You may perform this operation any number of times.
For example, if you have two towers containing discs $[6, 4, 2, 1]$ and $[8, 7, 5, 3]$ (in order from bottom to top), there are only two possible operations:
- move disc $1$ from the first tower to the second tower, so the towers are $[6, 4, 2]$ and $[8, 7, 5, 3, 1]$ ;
- move discs $[2, 1]$ from the first tower to the second tower, so the towers are $[6, 4]$ and $[8, 7, 5, 3, 2, 1]$ .
Let the difficulty of some set of towers be the minimum number of operations required to assemble one tower containing all of the discs. For example, the difficulty of the set of towers $[[3, 1], [2]]$ is $2$ : you may move the disc $1$ to the second tower, and then move both discs from the second tower to the first tower.
You are given $m - 1$ queries. Each query is denoted by two numbers $a_i$ and $b_i$ , and means "merge the towers $a_i$ and $b_i$ " (that is, take all discs from these two towers and assemble a new tower containing all of them in descending order of their radii from top to bottom). The resulting tower gets index $a_i$ .
For each $k \in [0, m - 1]$ , calculate the difficulty of the set of towers after the first $k$ queries are performed.
You would like to assemble one tower containing all of those discs. To do so, you may choose two different towers $i$ and $j$ (each containing at least one disc), take several (possibly all) top discs from the tower $i$ and put them on top of the tower $j$ in the same order, as long as the top disc of tower $j$ is bigger than each of the discs you move. You may perform this operation any number of times.
For example, if you have two towers containing discs $[6, 4, 2, 1]$ and $[8, 7, 5, 3]$ (in order from bottom to top), there are only two possible operations:
- move disc $1$ from the first tower to the second tower, so the towers are $[6, 4, 2]$ and $[8, 7, 5, 3, 1]$ ;
- move discs $[2, 1]$ from the first tower to the second tower, so the towers are $[6, 4]$ and $[8, 7, 5, 3, 2, 1]$ .
Let the difficulty of some set of towers be the minimum number of operations required to assemble one tower containing all of the discs. For example, the difficulty of the set of towers $[[3, 1], [2]]$ is $2$ : you may move the disc $1$ to the second tower, and then move both discs from the second tower to the first tower.
You are given $m - 1$ queries. Each query is denoted by two numbers $a_i$ and $b_i$ , and means "merge the towers $a_i$ and $b_i$ " (that is, take all discs from these two towers and assemble a new tower containing all of them in descending order of their radii from top to bottom). The resulting tower gets index $a_i$ .
For each $k \in [0, m - 1]$ , calculate the difficulty of the set of towers after the first $k$ queries are performed.
输入格式
The first line of the input contains two integers $n$ and $m$ ( $2 \le m \le n \le 2 \cdot 10^5$ ) — the number of discs and the number of towers, respectively.
The second line contains $n$ integers $t_1$ , $t_2$ , ..., $t_n$ ( $1 \le t_i \le m$ ), where $t_i$ is the index of the tower disc $i$ belongs to. Each value from $1$ to $m$ appears in this sequence at least once.
Then $m - 1$ lines follow, denoting the queries. Each query is represented by two integers $a_i$ and $b_i$ ( $1 \le a_i, b_i \le m$ , $a_i \ne b_i$ ), meaning that, during the $i$ -th query, the towers with indices $a_i$ and $b_i$ are merged ( $a_i$ and $b_i$ are chosen in such a way that these towers exist before the $i$ -th query).
The second line contains $n$ integers $t_1$ , $t_2$ , ..., $t_n$ ( $1 \le t_i \le m$ ), where $t_i$ is the index of the tower disc $i$ belongs to. Each value from $1$ to $m$ appears in this sequence at least once.
Then $m - 1$ lines follow, denoting the queries. Each query is represented by two integers $a_i$ and $b_i$ ( $1 \le a_i, b_i \le m$ , $a_i \ne b_i$ ), meaning that, during the $i$ -th query, the towers with indices $a_i$ and $b_i$ are merged ( $a_i$ and $b_i$ are chosen in such a way that these towers exist before the $i$ -th query).
输出格式
Print $m$ integers. The $k$ -th integer ( $0$ -indexed) should be equal to the difficulty of the set of towers after the first $k$ queries are performed.
输入输出样例
输入 #1
7 4 1 2 3 3 1 4 3 3 1 2 3 2 4
输出 #1
5 4 2 0
The towers in the example are:
- before the queries: $[[5, 1], [2], [7, 4, 3], [6]]$ ;
- after the first query: $[[2], [7, 5, 4, 3, 1], [6]]$ ;
- after the second query: $[[7, 5, 4, 3, 2, 1], [6]]$ ;
- after the third query, there is only one tower: $[7, 6, 5, 4, 3, 2, 1]$ .
- before the queries: $[[5, 1], [2], [7, 4, 3], [6]]$ ;
- after the first query: $[[2], [7, 5, 4, 3, 1], [6]]$ ;
- after the second query: $[[7, 5, 4, 3, 2, 1], [6]]$ ;
- after the third query, there is only one tower: $[7, 6, 5, 4, 3, 2, 1]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted