PAT (Advanced Level) Practice 1090 Highest Price in Supply Chain (25 分) 凌宸1642

PAT (Advanced Level) Practice 1090 Highest Price in Supply Chain (25 分) 凌宸1642

题目描述:

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one's supplier in a price P and sell or distribute them in a price that is r% higher than P. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the highest price we can expect from some retailers.

译:一条供应链是零售商,经销商和供应商的网络 -- 参与将产品从供应商转移到客户的每个人。

从一个根供应商开始,链上的每个人都以 P 的价格从自己的供应商那里购买产品,然后以比 P 高 r% 的价格出售或分销。 根供应商,没有供应周期。

现在给定一个供应链,你应该告诉我们可以从一些零售商那里得到的最高价格。


Input Specification (输入说明):

Each input file contains one test case. For each case, The first line contains three positive numbers: N (≤105), the total number of the members in the supply chain (and hence they are numbered from 0 to N−1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number Si is the index of the supplier for the i-th member. Sroot for the root supplier is defined to be −1. All the numbers in a line are separated by a space.

译:每个输入文件包含一个测试用例。 对于每种情况,第一行包含三个正数:N(≤105),供应链中成员的总数(因此它们的编号从 0 到 N-1); P,根供应商给出的价格; 和 r,每个分销商或零售商的价格增长百分比。 然后下一行包含N个数字,每个数字Si是第i个成员的供应商索引。 根供应商的 Sroot定义为 -1。 一行中的所有数字都用空格分隔。。


output Specification (输出说明):

For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 1010.

译:对于每个测试用例,在一行中打印出我们可以从某些零售商那里得到的最高价格,精确到小数点后两位,以及以最高价格销售的零售商数量。 两个数字之间必须有一个空格。 保证价格不超过1010


Sample Input (样例输入):

9 1.80 1.00
1 5 4 4 -1 4 5 3 6

Sample Output (样例输出):

1.85 2

The Idea:

其实就是问一颗树最底层的叶子节点有几个,并且位于第几层,然后根据层数,计算价格的复利即可得到最高价格。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
struct NODE{
	int level ;
	vector<int> child ;
}Node[100010];
int n , t , root , l = 0 , ans2 = 1  ; // ans2 的初始值为1 ,这是以为只有一个结点的时候,人数为 1
double p , r , ans1 ;
void levelOrder(int root){
	queue<int> q ;
	q.push(root) ;
	while(!q.empty()){
		int top = q.front() ;
		q.pop() ;
		if(Node[top].level != l){
			ans2 = 1 + q.size() ; // 更新当前层的人数
			l ++ ; // 更新层数
		}
		for(int i = 0 ; i < Node[top].child.size() ; i ++){
			int tem = Node[top].child[i] ;
			q.push(tem) ;
			Node[tem].level = Node[top].level + 1 ;
		}
	}
}
int main(){
	cin >> n >> p >> r;
	for(int i = 0 ; i < n ; i ++){
		cin >> t ;
		if(t == -1) root = i ;
		else Node[t].child.push_back(i) ;
	}
	Node[root].level = 0 ;
	levelOrder(root) ;
	ans1 = p * pow((1 +  r / 100) , l); // 按照复利计算最后的价格
	printf("%.2f %d\n" , ans1, ans2) ;
	return 0 ;
}

上一篇:PAT-甲级-1090 Highest Price in Supply Chain (25 分)


下一篇:1090 危险品装箱 (25 分)