BZOJ3835: [Poi2014]Supercomputer

本文探讨了一种新型超级计算机架构下,如何通过调整处理器单元数量来优化程序执行时间的问题。给定一棵指令树,文章介绍了如何计算不同处理器数量配置下的最小运行时间,并附带了具体的实现代码。

Description

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个未访问的点,且这些点的父亲必须在之前被访问过。

 

Input

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.

 

Output

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.

 

Sample Input

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

Sample Output

8

HINT

 

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

 

感觉还是可以意会结论的,然后斜率优化一下。
#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;
}

  

 

转载于:https://www.cnblogs.com/wzj-is-a-juruo/p/5418381.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值