BZOJ3835[Poi2014]Supercomputer——斜率优化
题目描述
输入
输出
样例输入
3
1 1 1 3 4 3 2 8 6 9 10 12 12 13 14 11 11 11 11
样例输出
提示
#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——斜率优化的更多相关文章
- BZOJ3835 [Poi2014]Supercomputer 【斜率优化】
题目链接 BZOJ3835 题解 对于\(k\),设\(s[i]\)为深度大于\(i\)的点数 \[ans = max\{i + \lceil \frac{s[i]}{k}\} \rceil\] 最优 ...
- BZOJ3835: [Poi2014]Supercomputer
Description Byteasar has designed a supercomputer of novel architecture. It may comprise of many (id ...
- 洛谷3571 POI2014 SUP-Supercomputer (斜率优化)
一道神仙好题. 首先看到有多组\(k\),第一反应就是离线. 考虑贪心. 我们每次一定是尽量选择有儿子的节点.以便于我们下一次扩展. 但是对于一个\(k\),每次贪心的复杂度是\(O(n)\) 总复杂 ...
- DP的各种优化(动态规划,决策单调性,斜率优化,带权二分,单调栈,单调队列)
前缀和优化 当DP过程中需要反复从一个求和式转移的话,可以先把它预处理一下.运算一般都要满足可减性. 比较naive就不展开了. 题目 [Todo]洛谷P2513 [HAOI2009]逆序对数列 [D ...
- BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4026 Solved: 1473[Submit] ...
- [斜率优化DP]【学习笔记】【更新中】
参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...
- BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9812 Solved: 3978[Submit][St ...
- 单调队列 && 斜率优化dp 专题
首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...
- 【BZOJ2442】 [Usaco2011 Open]修剪草坪 斜率优化DP
第一次斜率优化. 大致有两种思路: 1.f[i]表示第i个不选的最优情况(最小损失和)f[i]=f[j]+e[i] 显然n^2会T,但是可以发现f的移动情况可以用之前单调队列优化,就优化成O(n)的了 ...
随机推荐
- u-boot全面分析
uboot主Makefile分析1 uboot住Makefile分析参考:https://www.2cto.com/kf/201607/522424.html uboot version确定(Make ...
- MongoDB的地理位置查询,以及和mysql的使用对比
MongoDB的一个特色就是具有丰富的查询接口,比如地理位置查询. 在地理位置查询上,MongoDB有着比传统关系型数据库的优势,下面举个例子. 当前移动互联网应用,按用户离目标门店距离排序上的场景很 ...
- odoo之页面跳转
击备注时,会由备注id带出他的内容 customer.requirement这是备注内容表 def sale_requirements_change(self, cr, uid, ids, requi ...
- docker load导入镜像报错:open /var/lib/docker/tmp/docker-import-970689518/bin/json: no such file or directory
今天将之前打包好的mysql5.7.19的tar包通过docker load命令导入到Docker环境中却报出了如下错误: [root@host---- task]# docker load < ...
- Luogu4423 BJWC2011 最小三角形 平面最近点对
传送门 题意:给出$N$个点,求其中周长最小的三角形(共线的也计算在内).$N \leq 2 \times 10^5$ 这道题唤起了我对平面最近点对的依稀记忆 考虑平面最近点对的分治,将分界线两边的求 ...
- Dell Technology Summit(2018.10.17)
时间:2018.10.17地点:北京国家会议中心
- 一头雾水的"Follow The Pointer"
原文:一头雾水的"Follow The Pointer" 一头雾水的"Follow The Pointer" ...
- EF性能优化-有人说EF性能低,我想说:EF确实不如ADO.NET
十年河东,十年河西,莫欺少年穷. EF就如同那个少年,ADO.NET则是一位壮年.毕竟ADO.NET出生在EF之前,而EF所走的路属于应用ADO.NET. 也就是说:你所写的LINQ查询,最后还是要转 ...
- Awesome Python,Python的框架集合
Awesome Python A curated list of awesome Python frameworks, libraries and software. Inspired by awes ...
- CentOS6下OpenLDAP+PhpLdapAdmin基本安装及主从/主主高可用模式部署记录
下面测试的部署机ip地址为:192.168.10.2051)yum安装OpenLDAP [root@openldap-server ~]# yum install openldap openldap- ...