EagleBear2002 的博客

这里必须根绝一切犹豫,这里任何怯懦都无济于事

P3571 [POI 2014] SUP-Supercomputer

题目链接:P3571 [POI 2014] SUP-Supercomputer - 洛谷

本题是经典斜率优化题型。

题目描述

Byteasar has designed a supercomputer of novel architecture.

1
It may comprise of many (identical) processing units.

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, n and q (1n,q1 000 000), separated by a single space, that specify the number of instructions in Byteasar's program and the number of running time queries (for different numbers of processing units).

In the second line of input, there is a sequence of q integers, k1,k2,,kq (1ki1 000 000), separated by single spaces: ki is the number of processing units in Byteasar's i-th query.

In the third and last input line, there is a sequence of n1 integers, a2,a3,,an(1ai<i) separated by single spaces: ai specifies the number of the parent instruction of the instruction number i. The instructions are numbered with successive integers from 1 to n, where the instruction no. 1 is the first instruction of the program.

标准输入的第一行包含两个整数 nq1n,q1 000 000),两个整数之间用一个空格隔开,分别表示 Byteasar 程序中的指令数和运行时间查询的数量(针对不同数量的处理单元)。

输入的第二行包含一个 q 个整数序列,k1,k2,,kq1ki1 000 000),两个整数之间用一个空格隔开:ki 表示 Byteasar 第 i 个查询中的处理单元数量。

在第三行(也是最后一行)输入中,有一个由 n1 个整数组成的序列,a2,a3,,an1ai<i),每个整数之间用单个空格分隔:ai 表示指令号 i 的父指令编号。这些指令使用从 1 到 n 的连续整数编号,其中指令号 1 是程序的第一条指令。

输出格式

Your program should print one line consisting of q integers, separated by single spaces, to the standard output:

the i-th of these numbers should specify the minimum execution time of the program on a supercomputer with ki processing units.

你的程序应该在标准输出中打印一行由 q 个整数组成,每个整数之间用空格分隔:

这些数字中的第 i 个应该指定该程序在具有 ki 个处理单元的超级计算机上的最小执行时间。

输入输出样例 #1

输入 #1

1
2
3
20 1
3
1 1 1 3 4 3 2 8 6 9 10 12 12 13 14 11 11 11 11

输出 #1

1
8

题解

题意转化

将“父节点已被访问的节点才能被访问”这一访问节点的操作转化为从树上删除叶子结点的操作。现在题意转化为:每次可以删除至多 k 个叶子结点,最少多少次操作可以删除整棵树?

方案构造

很容易想到这一贪心方案:

情况 操作
1 当前叶子结点至少有 k 删除 k 个最深的结点
2 当前叶子结点少于 k 删除当前所有叶子结点(只能删除一层叶子)

在询问 k 下,令 hk 表示情况 2 第一次出现时树的深度;shk+1 表示深度不小于 hk+1 的节点个数。

显然会有 shk+1k 次操作 1,此后 hk 次操作 2。我们只要计算出合适的 hk 就能找到对应的 f(k)=hk+shk+1k

直接给出结论:

hk=argmax0hH{h+sh+1k}f(k)=hk+shk+1k=maxh{h+sh+1k}

也就是说,使得 h+sh+1k 最大的 h 就是我们要找的 hk。正确性证明如下:

  • 如果枚举的 h 过大,即 h>hk,会比 hk 更早按照进行操作 2,此时叶子结点必然多于 k 个。此时直接操作 2“删除当前所有叶子结点”是不合法操作,且会导致总操作数 h+sh+1k 更小;
  • 如果枚举的 h 过小,即 h<hk,会比 hk 更晚按照情况 2 操作,必然意味着先前叶子结点不足 k 个时进行了操作 1“k 个最深的节点”是不合法操作,且会导致总操作数 h+sh+1k 更小。

因此,只有合法的 h=hk 时,总操作数表达式 h+sh+1k 取到最大值,即 hk=argmax0hH{h+sh+1k}

斜率优化

将方程进一步变形(为了与斜率 k 不重名,将询问用 i 表示):

f(i)=maxh{h+sh+1i}=maxh{ih+sh+1i}

只要设法让 ih+sh+1 最大即可。令:

bi=ih+sh+1yh=sh+1xh=hki=i

显然 ki 随着 i 的增加而单调减少。维护一个上凸壳即可完成均摊 O(1) 的状态转移。