BZOJ3835[Poi2014]Supercomputer——斜率优化

本文探讨了一种新型超级计算机的架构,通过树状结构的程序设计,实现了指令的并行执行。文章深入分析了如何根据不同的处理器单元数量,优化程序的运行时间,提出了最优策略,并提供了具体的算法实现。

题目描述

Byteasar has designed a supercomputer of novel architecture. 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.
给定一棵N个节点的有根树,根节点为1。
Q次询问,每次给定一个K,用最少的操作次数遍历完整棵树,输出最少操作次数。
每次操作可以选择访问不超过K个未访问的点,且这些点的父亲必须在之前被访问过。

输入

In the first line of standard input, there are two integers, N and Q (1<=N,Q<=1 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 (1<=Ki<=1 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 N-1 integers, A2,A2…An (1<=Ai<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.

输出

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.

样例输入

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

样例输出

8

提示

1
2
3
4
5
6
7
8
1    
2 3 4
5 6 7
8 10  
9 12  
11 13 14
15 16 17
18 19 20
 
最优情况一定是每次选满k个,但这显然不能实现,因此最优策略就是每次尽可能多的选点且保证下一次也能尽可能多的选点。
那么对于每一次选点,能选子节点就选子节点,而不是选完这一层再选下一层,因为只要不到最底层,选子节点至少不会使下一次能选的点数变小。
当往下选不了了再回来选之前剩下的,这样的话前面一些层每层要选一次,后面的层要用size/k次。
能够证明出来合法的最优解是ans=max{i+i/k},其中i代表深度。
这样求每次询问都是O(n)的显然不行。但可以发现有些i永远不可能成为答案或者如果当前k时不能作为答案之后的k就一定不会成为答案。
因此可以斜率优化成O(n)。只处理出1<=k<=n的k的答案,剩下k>n的答案就是最大深度
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int head[1000010];
int n,Q,x;
int tot;
int k[1000010];
int to[1000010];
int next[1000010];
int dep;
int sum[1000010];
int c[1000010];
int q[1000010];
int ans[1000010];
int l=1,r;
void add(int x,int y)
{
    tot++;
    next[tot]=head[x];
    head[x]=tot;
    to[tot]=y;
}
void dfs(int x,int d)
{
    dep=max(dep,d);
    c[d]++;
    for(int i=head[x];i;i=next[i])
    {
        dfs(to[i],d+1);
    }
}
int calc(int x,int y)
{
    return x/y+(x%y>0);
}
int main()
{
    scanf("%d%d",&n,&Q);
    for(int i=1;i<=Q;i++)
    {
        scanf("%d",&k[i]);
    }
    for(int i=2;i<=n;i++)
    {
        scanf("%d",&x);
        add(x,i);
    }
    dfs(1,1);
    for(int i=dep;i>=0;i--)
    {
        sum[i]=sum[i+1]+c[i+1];
    }
    for(int i=dep;i>=0;q[++r]=i--)
    {
        while(l<r&&1ll*(q[r-1]-q[r])*(sum[i]-sum[q[r]])>=1ll*(q[r]-i)*(sum[q[r]]-sum[q[r-1]]))
        {
            r--;
        }
    }    
    for(int i=n;i>=1;i--)
    {
        while(l<r&&1ll*i*(q[l]-q[l+1])<=1ll*(sum[q[l+1]]-sum[q[l]]))
        {
            l++;
        }
        ans[i]=q[l]+calc(sum[q[l]],i);
    }
    for(int i=1;i<=Q;i++)
    {
        k[i]>n?printf("%d",dep):printf("%d",ans[k[i]]);
        if(i!=Q)
        {
            printf(" ");
        }
    }
}

转载于:https://www.cnblogs.com/Khada-Jhin/p/9681408.html

### 回答1: bzoj[1597][usaco2008 mar]土地购买 斜率优化 这道题是一道经典的斜率优化题目,需要用到单调队列的思想。 首先,我们可以将题目中的式子进行变形,得到: f[i] = f[j] + (sum[i] - sum[j] - m) ^ 2 + k 其中,sum[i] 表示前缀和,m 和 k 都是常数。 我们可以将式子中的 sum[i] 和 k 看作常数,那么我们需要优化的就是 (sum[i] - sum[j] - m) ^ 2 这一项。 我们可以将其展开,得到: (sum[i] - sum[j] - m) ^ 2 = sum[i] ^ 2 - 2 * sum[i] * (sum[j] + m) + (sum[j] + m) ^ 2 我们可以将其看作一个二次函数,其中 a = 1,b = -2 * (sum[j] + m),c = (sum[j] + m) ^ 2。 我们可以发现,当 j < k 时,如果 f[j] + a * sum[j] + b * sum[j] <= f[k] + a * sum[k] + b * sum[k],那么 j 就不可能是最优决策点,因为 k 比 j 更优。 因此,我们可以用单调队列来维护决策点。具体来说,我们可以维护一个单调递增的队列 q,其中 q[i] 表示第 i 个决策点的下标。每次加入一个新的决策点 i 时,我们可以将队列尾部的决策点 j 弹出,直到队列为空或者 f[j] + a * sum[j] + b * sum[j] <= f[i] + a * sum[i] + b * sum[i]。然后,我们将 i 加入队列尾部。 最后,队列头部的决策点就是最优决策点。我们可以用类似于双指针的方法来维护队列头部的决策点是否在当前区间内,如果不在,就弹出队列头部。 时间复杂度为 O(n)。 ### 回答2: 这道题目属于斜率优化的经典题目,难度较高,需要掌握一定的数学知识。 首先,我们可以将题目中的“最大利润”转化为“最小成本”,这样问题就变成了找到一个方案,使得购买土地的成本最小。 接着,我们考虑如何用斜率优化来解决这个问题。我们可以定义一个函数f(i),表示前i块土地的最小成本。 显然,f(1)=0,因为不需要购买任何土地。 对于f(i),它可以由f(j)+b(i)×a(j+1)得到,其中j<i,a(j+1)表示第j+1块土地的面积,b(i)表示第i块土地的价格。这个式子的含义是,我们现在要购买第i块土地,那么前面的土地(即前j块)就都要买,所以f(j)表示前j块土地的最小成本,b(i)×a(j+1)表示购买第i块土地的成本。 那么,我们可以得到递推公式: f(i)=min{f(j)+b(i)×a(j+1)},其中j<i。 这个公式看起来很简单,但是要注意的是,当b(i)×a(j+1)的斜率相同时,我们需要取其中面积较小的土地,因为它的价格更低。因此,我们需要对斜率进行排序,并在递推中用单调队列维护斜率相等的情况下面积最小的土地。 最终,f(n)就是题目所求的最小成本。 总之,这道题目需要深入理解斜率优化算法的原理和实现方式,并且需要注意细节处理,如果能够顺利地解决这个问题,那么对于斜率优化算法的掌握程度就有了很大的提升。 ### 回答3: 土地购买问题可以采用斜率优化算法来解决。这个问题可以转化为一个单调队列的问题。 首先,我们需要对土地价格按照边长从小到大排序。然后,对于每块土地,我们需要求出它的贡献。设 $f_i$ 表示前 $i$ 块土地连续的最小代价。 设当前处理到第 $i$ 块土地,已经求出了前 $j$ 块土地的最小代价 $f_j$。那么我们可以得到下面这个式子: $$ f_i=\min\limits_{j=1}^{i-1}\{f_j+(S_i-S_j)^2+P\} $$ 式子中,$S_i$ 表示前 $i$ 块土地的边长和,$P$ 表示额外购买土地的代价。首先,不考虑额外购买土地,我们可以使用动态规划来求出 $f_i$。但是,考虑到额外购买土地的代价 $P$ 是一个固定值,我们可以考虑将它与某一块土地的代价合并起来,这样就可以使用斜率优化技术来优化动态规划算法。 我们定义一个决策点 $j$,表示我们当前要处理第 $i$ 块土地时,已经处理过 $j$ 块土地,并将第 $j+1$ 块土地到第 $i$ 块土地购买,所需的最小代价。我们假设 $S_i>S_j$,则可以得到下面这个式子: $$ f_i=\min\limits_{j=1}^{i-1}\{f_j+(S_i-S_j)^2+P\} $$ 将它整理成斜率截距式可以得到: $$ y=kx+b $$ 其中 $k=(S_j)^2-2S_iS_j$,$b=f_j+(S_i)^2+P-S_j^2$,$x=S_j$,$y=f_j+(S_j-S_i)^2-S_j^2$。我们发现 $k$ 是一个单调递减的函数,因此我们可以使用一个单调队列来维护所有可能成为决策点的点。对于每个点,我们计算函数 $y$ 的值并将它们加入队列,然后取队头元素的值作为 $f_i$。 综上所述,我们可以使用斜率优化技术来解决土地购买问题,时间复杂度为 $O(n)$。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值