BZOJ3835 [Poi2014]Supercomputer 【斜率优化】
题目链接
题解
对于\(k\),设\(s[i]\)为深度大于\(i\)的点数
\]
最优决策一定是一开始每一层拿不满\(k\)个点,然后之后一直往下拿的同时通过中间层剩余的点拿满\(k\)个点
我们就有前\(i\)层用了\(i\)次,后面每\(k\)个点用一次
容易证明合法的\(i\)得出的答案一定是最大的
然后式子化为
\]
就是求\(ik + s[i]\)最大
斜率优化即可
复杂度\(O(n)\)
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 1000005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,Q,s[maxn],K[maxn],ls[maxn],rb[maxn],fa[maxn],dep[maxn];
int ans[maxn],q[maxn],head,tail,D;
void dfs(int u){
s[dep[u]]++; D = max(D,dep[u] + 1);
for (int k = ls[u]; k; k = rb[k]){
dep[k] = dep[u] + 1;
dfs(k);
}
}
inline int C(int a,int b){return a / b + (a % b > 0);}
int main(){
n = read(); Q = read();
REP(i,Q) K[i] = read();
for (int i = 2; i <= n; i++) fa[i] = read(),rb[i] = ls[fa[i]],ls[fa[i]] = i;
dfs(1);
for (int i = D; ~i; i--) s[i] += s[i + 1];
head = 0; tail = -1;
for (int i = D; ~i; i--){
while (head < tail && 1ll * (i - q[tail]) * (s[q[tail]] - s[q[tail - 1]]) >= 1ll * (q[tail] - q[tail - 1]) * (s[i] - s[q[tail]])) tail--;
q[++tail] = i;
}
for (int i = n; i; i--){
while (head < tail && (s[q[head + 1]] - s[q[head]]) >= 1ll * i * (q[head] - q[head + 1])) head++;
ans[i] = q[head] + C(s[q[head]],i);
}
REP(i,Q) printf("%d",K[i] > n ? D : ans[K[i]]),i < Q ? putchar(' ') : 0;
return 0;
}
BZOJ3835 [Poi2014]Supercomputer 【斜率优化】的更多相关文章
- BZOJ3835[Poi2014]Supercomputer——斜率优化
题目描述 Byteasar has designed a supercomputer of novel architecture. It may comprise of many (identical ...
- 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)的了 ...
随机推荐
- tomcat 项目的搭建-【Linux】
- Flink BLOB架构
Flink中支持的BLOB文件类型 jar包 被user classloader使用的jar包 高负荷RPC消息 1. RPC消息长度超出了akka.framesize的大小 2. 在HA摸式中,利用 ...
- 开源ITIL管理软件iTop 2.5-2.6安装
环境说明 : 操作系统centos 7.itop版本 iTop-2.5.0-3935.数据库:mariadb iTop 2.5只支持PHP5.6以上版本,本例安装的是php72w版本 1.下载链接: ...
- C++判断回文
判断一个字符串是否为回文,如“goddog”. 代码: #include <iostream> #include <string> #include <stdio.h&g ...
- [转载] Ubuntu 12.04下安装git,SSH及出现的Permission denied解决办法
如何安装ssh http://os.51cto.com/art/201109/291634.htm 仅需要阅读至成功开启ssh服务即可 http://www.linuxidc.com/Linux/20 ...
- [译文]c#扩展方法(Extension Method In C#)
原文链接: https://www.codeproject.com/Tips/709310/Extension-Method-In-Csharp 介绍 扩展方法是C# 3.0引入的新特性.扩展方法使你 ...
- (转)一个简单的rest_framework demo
转发:https://www.cnblogs.com/fu-yong/p/9100559.html models.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 from ...
- 作业1MathExam
自己取一个大气又可爱的标题 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 720 1000 ...
- 实验3 --俄罗斯方块 with 20135335郝爽
一. 实验内容 (一)敏捷开发与XP 内容:1.敏捷开发(Agile Development)是一种以人为核心.迭代.循序渐进的开发方法. 2.极限编程(eXtreme Programming,X ...
- “四则运算生成程序——GUI支持和部分功能改进”链接
项目成员:张金生 张政 <正文随笔发布在张金生博客中> 四则运算生成程序——GUI支持和部分功能改进