BZOJ3835: [Poi2014]Supercomputer
Description
Input
Output
Sample Input
3
1 1 1 3 4 3 2 8 6 9 10 12 12 13 14 11 11 11 11
Sample Output
HINT
#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
return x*f;
}
typedef long long ll;
const int maxn=1000010;
int n,m,d,A[maxn],h[maxn],s[maxn],st[maxn];
int Q[maxn],ans[maxn];
int main() {
n=read();m=read();h[1]=1;
rep(i,1,m) A[i]=read();
rep(i,2,n) d=max(d,h[i]=h[read()]+1);
rep(i,1,n) s[h[i]]++;
dwn(i,d,1) s[i]+=s[i+1];
rep(i,1,d) s[i]=s[i+1];
int l=1,r=0;
dwn(i,d,0) {
while(l<r&&(ll)(Q[r-1]-Q[r])*(s[i]-s[Q[r]])>=(ll)(Q[r]-i)*(s[Q[r]]-s[Q[r-1]])) r--;
Q[++r]=i;
}
dwn(i,n,1) {
while(l<r&&(ll)i*(Q[l]-Q[l+1])<=s[Q[l+1]]-s[Q[l]]) l++;
ans[i]=Q[l]+(s[Q[l]]?((s[Q[l]]-1)/i+1):0);
}
rep(i,1,m) printf("%d%c",ans[min(n,A[i])],i==m?'\n':' ');
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 【斜率优化】
题目链接 BZOJ3835 题解 对于\(k\),设\(s[i]\)为深度大于\(i\)的点数 \[ans = max\{i + \lceil \frac{s[i]}{k}\} \rceil\] 最优 ...
- 【BZOJ】3835: [Poi2014]Supercomputer
题意 \(n(1 \le 1000000)\)个点的有根树,\(1\)号点为根,\(q(1 \le 1000000)\)次询问,每次给一个\(k\),每一次可以选择\(k\)个未访问的点,且父亲是访问 ...
- 题解-POI2014 Supercomputer
Problem 辣鸡bzoj权限题,洛谷链接 题意概要:一棵 \(n\) 个点有根树.\(Q\) 次询问给出一个 \(K\),回答遍历完整棵树所需最少操作次数.每次操作可以选择访问不超过 \(K\) ...
- [POI2014]Supercomputer
题目大意: 给定一个$n(n\le10^6)$个结点的有根树,从根结点开始染色.每次可以染和已染色结点相邻的任意$k$个结点.$q(q\le10^6)$组询问,每次给定$k$,问至少需要染几次? 思路 ...
- POI2014题解
POI2014题解 [BZOJ3521][Poi2014]Salad Bar 把p当作\(1\),把j当作\(-1\),然后做一遍前缀和. 一个合法区间\([l,r]\)要满足条件就需要满足所有前缀和 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- BZOJ 3524: [Poi2014]Couriers [主席树]
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1892 Solved: 683[Submit][St ...
- BZOJ 3524: [Poi2014]Couriers
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1905 Solved: 691[Submit][St ...
随机推荐
- 【数据结构】建立和平衡AVL二叉树
一步一步写平衡二叉树(AVL树) 原文地址:http://www.cppblog.com/cxiaojia/archive/2012/08/20/187776.html 我添加了一些内容,以充实整个算 ...
- [LeetCode] Word Break II (TLE)
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- Solr入门之(8)中文分词器配置
Solr中虽然提供了一个中文分词器,但是效果很差,可以使用IKAnalyzer或Mmseg4j 或其他中文分词器. 一.IKAnalyzer分词器配置: 1.下载IKAnalyzer(IKAnalyz ...
- go sample - mongodb
简单的mongodb 操作 package mainimport ( "fmt" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2 ...
- html5 svg
html5 svg <html > <body> <p>canvas 用js 绘画,是整幅画布,适合游戏 svg可放大,支持dom 操作,js事件 线性渐变.高斯模 ...
- Reading and Writing CSV Files in C#
Introduction A common requirement is to have applications share data with other programs. Although t ...
- Codeforces Round #369 (Div. 2) D. Directed Roads dfs求某个联通块的在环上的点的数量
D. Directed Roads ZS the Coder and Chris the Baboon has explored Udayland for quite some time. The ...
- 【SQL Sever】实现SQL Sever的发布。订阅。 双机热备
实现SQL Sever的发布和订阅 最大的好处就是: 可以实现读写分离,增删改操作在主数据库服务器上进行,查询在备份数据库服务器上进行.一方面提高软件执行效率,另一方面也减轻主库压力. 本次实现发布 ...
- 当 NSDictionary 遇见 nil
Demo project: NSDictionary-NilSafe 问题 相信用 Objective-C 开发 iOS 应用的人对下面的 crash 不会陌生: *** -[__NSPlacehol ...
- iOS UIImageView设置为圆形
UIImageView设置为圆形的方法(效率比较低下,当需要显示很多圆形view的时候,非常不推荐这种方式): imageView.layer.masksToBounds = YES; imageVi ...