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.

Input Specification:

Each input file contains one test case. For each case, The first line contains three positive numbers: N (≤), 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 S​i​​ is the index of the supplier for the i-th member. S​root​​ for the root supplier is defined to be −. All the numbers in a line are separated by a space.

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 1.

Sample Input:

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

Sample Output:

1.85 2

题意:

给出商品的供应链,每一条链之间都会提高r%(相对于进价而言),找到最高的价格,也就是找到最长的供应链,并指出最长的供应链有几条。

思路:

这种题目如果用对数据结构的话会很简单,这一题用到了queue和map,map给出谁是供应者,谁是被供应者,queue用来模拟层次遍历,最长的供应链,也就是最大的层数。

Code:

#include<iostream>
#include<map>
#include<iomanip>
#include<vector>
#include<queue> using namespace std; int main() {
int n;
double p, r;
cin >> n >> p >> r; int t;
vector<int> chain(n);
for (int i = 0; i < n; ++i) {
cin >> t;
chain[i] = t;
}
map<int, vector<int> > mp;
int last;
for (int i = 0; i < n; ++i) {
last = chain[i];
mp[last].push_back(i);
} auto it = mp.find(-1);
vector<int> v = it->second;
queue<int> q;
for (int i = 0; i < v.size(); ++i)
q.push(v[i]);
q.push(-1);
int len = 0, num = 0;
while(!q.empty()) {
if (q.size() == 1) break;
int f = q.front();
q.pop();
num++;
if (f == -1) {
len++;
num = 0;
q.push(-1);
} else {
if (mp.find(f) != mp.end()) {
v = mp.find(f)->second;
for (int i = 0; i < v.size(); ++i)
q.push(v[i]);
}
}
} double heighPrice = p;
for (int i = 0; i < len; ++i) {
heighPrice *= (1 + r/100);
} cout << fixed << setprecision(2) << heighPrice << " " << num << endl; return 0;
}

  

看到全部的数据都通过了,也可以睡觉了。

1090 Highest Price in Supply Chain的更多相关文章

  1. [建树(非二叉树)] 1090. Highest Price in Supply Chain (25)

    1090. Highest Price in Supply Chain (25) A supply chain is a network of retailers(零售商), distributors ...

  2. PAT 1090 Highest Price in Supply Chain[较简单]

    1090 Highest Price in Supply Chain(25 分) A supply chain is a network of retailers(零售商), distributors ...

  3. 1090 Highest Price in Supply Chain——PAT甲级真题

    1090 Highest Price in Supply Chain A supply chain is a network of retailers(零售商), distributors(经销商), ...

  4. 1090. Highest Price in Supply Chain (25)

    时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...

  5. 1090. Highest Price in Supply Chain (25) -计层的BFS改进

    题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...

  6. PAT 甲级 1090 Highest Price in Supply Chain

    https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944 A supply chain is a ne ...

  7. 1090 Highest Price in Supply Chain (25 分)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  8. PAT 1090. Highest Price in Supply Chain

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  9. 1090 Highest Price in Supply Chain (25)(25 分)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  10. PAT Advanced 1090 Highest Price in Supply Chain (25) [树的遍历]

    题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)–everyone inv ...

随机推荐

  1. Android 7.0 TextView点击事件无效修复方案

    public class PostTextView extends TextView { private Handler handler = new Handler(Looper.getMainLoo ...

  2. 代码安全性和健壮性:如何在if和assert中做选择?

    道哥的第 023 篇原创 目录 一.前言 二.assert 断言 assert 是一个宏,不是一个函数 三.if VS assert 1. 使用 if 语句来检查 2. 使用 assert 断言来检查 ...

  3. MHA架构搭建中遇到的问题

    1. 两个包:mha4mysql-manager-0.56-0.el6.noarch.rpm 和 mha4mysql-node-0.56-0.el6.norch.rpm 地址:https://code ...

  4. 腾讯云容器服务 TKE 拿下新加坡 MTCS 最高级别安全认证

    近日,腾讯云容器服务 TKE 荣获新加坡 MTCS 最高级安全认证,标志着腾讯云 TKE 在为用户提供可靠.易部署.灵活扩展等基础服务上,已经全面满足了新加坡监管机构以及多个行业客户对服务安全的要求. ...

  5. C语言中指针和多维数组

    指针和多维数组 数组名是特殊的指针 数组是一个特殊的指针,多维数组也是更为复杂的数组,它们的关系是什么样的呢? 我们通过一个简单的例子来比较形象的了解指针和多维数组: int a[2][3]; 这是一 ...

  6. Springboot的监控

    目录 Micrometer 计数器 仪表 摘要 计时器 Prometheus grafana 保存后我们就能在dashboard上看得我我们的监控指标了参考 Spring Boot有个子项目Sprin ...

  7. 2019HDU多校第一场 6582 Path 【最短路+最大流最小割】

    一.题目 Path 二.分析 首先肯定要求最短路,然后如何确定所有的最短路其实有多种方法. 1 根据最短路,那么最短路上的边肯定是可以满足$dist[from] + e.cost = dist[to] ...

  8. Linux 自定义快捷命令

    Linux中一些比较常用的命令总是重复敲很麻烦,这个时候就可以使用 alias 来自定义快捷命令,用以简化操作.系统会有一些预定义的快捷命令,比如 ll 的效果就和 ls -l 一样. 可以使用 al ...

  9. python3 输出字符

    import string'''whitespace -- a string containing all ASCII whitespaceascii_lowercase -- a string co ...

  10. Ubuntu20.04linux内核(5.4.0版本)编译准备与实现过程-编译前准备(1)

    最近项目也和linux kernel技术有关,调试内核和内核模块.修改内核源码,是学习内核的重要技术手段之一.应用这些技术时,都有一本基本的要求,那就是编译内核.因此,在分析内核调试技术之前,本随笔给 ...