题目链接:P3571 [POI 2014] SUP-Supercomputer - 洛谷。
本题是经典斜率优化题型。
题目描述
Byteasar has designed a supercomputer of novel architecture.
1 |
|
Each processing unit can execute a single instruction per time unit.
The programs for this computer are not sequential but rather have a tree structure.
Each instruction may have zero, one, or multiple subsequent instructions, for which it is the parent instruction.
The instructions of the program can be executed in parallel on all available processing units. Moreover, they can be executed in many orders: the only restriction is that an instruction cannot be executed unless its parent instruction has been executed before. For example, as many subsequent instructions of an instruction that has been executed already can be executed in parallel as there are processing units.
Byteasar has a certain program to run. Since he likes utilizing his resources optimally, he is wondering how the number of processing units would affect the running time.
He asks you to determine, for a given program and number of processing units, the minimum execution time of the program on a supercomputer with this many processing units.
Byteasar 设计了一台架构新颖的超级计算机。
1 |
|
每个处理单元每个时间单位可以执行一条指令。
这台计算机的程序不是顺序的,而是树形结构。
每条指令可能有零条、一条或多条后续指令,这些后续指令是它的父指令。
该程序的指令可以在所有可用的处理单元上并行执行。此外,它们可以按多种顺序执行:唯一的限制是,除非父指令之前已经执行过,否则指令无法执行。例如,一条已经执行过的指令的后续指令可以并行执行的数量与处理单元的数量相同。
Byteasar 有一个要运行的程序。由于他喜欢以最佳方式利用资源,因此他想知道处理单元的数量会如何影响运行时间。
他要求你确定,对于给定的程序和处理单元的数量,该程序在具有这么多处理单元的超级计算机上的最短执行时间。
输入格式
In the first line of standard input, there are two integers,
In the second line of input, there is a sequence of
In the third and last input line, there is a sequence of
标准输入的第一行包含两个整数
输入的第二行包含一个
在第三行(也是最后一行)输入中,有一个由
输出格式
Your program should print one line consisting of
the
你的程序应该在标准输出中打印一行由
这些数字中的第
输入输出样例 #1
输入 #1
1 |
|
输出 #1
1 |
|
题解
题意转化
将“父节点已被访问的节点才能被访问”这一访问节点的操作转化为从树上删除叶子结点的操作。现在题意转化为:每次可以删除至多
方案构造
很容易想到这一贪心方案:
情况 | 操作 | |
---|---|---|
1 | 当前叶子结点至少有 |
删除 |
2 | 当前叶子结点少于 |
删除当前所有叶子结点(只能删除一层叶子) |
在询问
显然会有
直接给出结论:
也就是说,使得
- 如果枚举的
过大,即 ,会比 更早按照进行操作 2,此时叶子结点必然多于 个。此时直接操作 2“删除当前所有叶子结点”是不合法操作,且会导致总操作数 更小; - 如果枚举的
过小,即 ,会比 更晚按照情况 2 操作,必然意味着先前叶子结点不足 个时进行了操作 1“ 个最深的节点”是不合法操作,且会导致总操作数 更小。
因此,只有合法的
斜率优化
将方程进一步变形(为了与斜率
只要设法让
显然