题目描述

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(" ");
}
}
}

BZOJ3835[Poi2014]Supercomputer——斜率优化的更多相关文章

  1. BZOJ3835 [Poi2014]Supercomputer 【斜率优化】

    题目链接 BZOJ3835 题解 对于\(k\),设\(s[i]\)为深度大于\(i\)的点数 \[ans = max\{i + \lceil \frac{s[i]}{k}\} \rceil\] 最优 ...

  2. BZOJ3835: [Poi2014]Supercomputer

    Description Byteasar has designed a supercomputer of novel architecture. It may comprise of many (id ...

  3. 洛谷3571 POI2014 SUP-Supercomputer (斜率优化)

    一道神仙好题. 首先看到有多组\(k\),第一反应就是离线. 考虑贪心. 我们每次一定是尽量选择有儿子的节点.以便于我们下一次扩展. 但是对于一个\(k\),每次贪心的复杂度是\(O(n)\) 总复杂 ...

  4. DP的各种优化(动态规划,决策单调性,斜率优化,带权二分,单调栈,单调队列)

    前缀和优化 当DP过程中需要反复从一个求和式转移的话,可以先把它预处理一下.运算一般都要满足可减性. 比较naive就不展开了. 题目 [Todo]洛谷P2513 [HAOI2009]逆序对数列 [D ...

  5. BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4026  Solved: 1473[Submit] ...

  6. [斜率优化DP]【学习笔记】【更新中】

    参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...

  7. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

  8. 单调队列 && 斜率优化dp 专题

    首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...

  9. 【BZOJ2442】 [Usaco2011 Open]修剪草坪 斜率优化DP

    第一次斜率优化. 大致有两种思路: 1.f[i]表示第i个不选的最优情况(最小损失和)f[i]=f[j]+e[i] 显然n^2会T,但是可以发现f的移动情况可以用之前单调队列优化,就优化成O(n)的了 ...

随机推荐

  1. easyui的datagrid的列checkbox自定义增加disabled选项

    需求根据权限判断datagrid的每一列的checkBox是否可选,看了下文档,发现editor的checkbox应该能实现这个功能,但我们项目自己将easyui外面包了一层,把原生的editor改成 ...

  2. 7-(基础入门篇)关于STM32底层程序使用说明

    https://www.cnblogs.com/yangfengwu/p/9357695.html 基础教程源码链接请在淘宝介绍中下载,由于链接很容易失效,如果失效请联系卖家,谢谢 https://i ...

  3. ASP.NET的生命周期

    我主要参考了这些文章 ASP.NET应用程序与页面生命周期, IIS处理Asp.net请求和 Asp.net页面生命周期 asp.net页面的生命周期 页面生命周期开始 (一)页面生命周期的主要阶段包 ...

  4. CF797F Mice and Holes 贪心、栈维护DP

    传送门 首先\(\sum c\)有些大,考虑将其缩小降低难度 考虑一个贪心:第一次所有老鼠都进入其左边第一个容量未满的洞(如果左边没有就进入右边第一个未满的洞),第二次所有老鼠都进入其右边第一个容量未 ...

  5. LiveCharts文档-4基本绘图-2基本柱形图

    原文:LiveCharts文档-4基本绘图-2基本柱形图 4基本绘图-2基本柱形图 using System.Windows.Forms; using LiveCharts; using LiveCh ...

  6. php WNMP(Windows+Nginx+Mysql+php)配置笔记

    下载安装 php 修改nginx 文件 参考云盘实例 eclipse php配置服务ip 127.0.0.1:999 以及项目路径(php解析路径)

  7. Zookeeper-集群与单机实践

    我用的是linux,CentOS7.3,zookeeper的版本是3.4.6,工具XShell.上传zookeeper的压缩包后我们开始操作. 集群模式: 1.解压zookeeper,路径随意 tar ...

  8. Luogu P1477 [NOI2008]假面舞会

    一道非常神奇的图论题解法无比新奇清新 我们首先把图分成三种情况: 有环的,此时答案一定是环长的因数(否则不能满足题意) 存在入度大于1的DAG图的 一棵树/一条链 很容易发现,最后一种情况想怎么取就怎 ...

  9. Unity3d之树木创建的参数设定

    Unity3d之树木创建的参数设定 通常Unity3d创建树木经常会创建出很多奇葩的种类=_=,以下是创建出比较正常树木的基本参数 1:> 基本树干形状建立: 选择根建立分枝干设置分支干Di ...

  10. 分布式全文搜索引擎ElasticSearch

    一 什么是 ElasticSearch Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elas ...