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. vue3 一些关键属性

    环境搭建 尤大开发了一个项目构建工具vite npm init vite-app <project-name> cd <project-name> npm install np ...

  2. 如何读写拥有命名空间xmlns 属性的Xml文件(C#实现)

    我们在进行C#项目Xml读写开发时经常遇到一些读写问题,今天我要介绍的是遇到多个命名空间xmlns属性时如何读写此类文件. 比如下面这个Xml文件: <?xml version="1. ...

  3. [个人总结]pytorch中用checkpoint设置恢复,在恢复后的acc上升

    原因是因为checkpoint设置好的确是保存了相关字段.但是其中设置的train_dataset却已经走过了epoch轮,当你再继续训练时候,train_dataset是从第一个load_data开 ...

  4. redis基础:redis下载安装与配置,redis数据类型使用,redis常用指令,jedis使用,RDB和AOF持久化

    知识点梳理 课堂讲义 课程计划 1. REDIS 入 门 (了解) (操作)   2. 数据类型 (重点) (操作) (理解) 3. 常用指令   (操作)   4. Jedis (重点) (操作) ...

  5. IDEA中便捷内存数据库H2的最简使用方式

    在IDEA中有时候为了练习,需要使用到数据库,但如果自己工作或开发机子上本来没有安装数据库,也没有可用的远程数据库时,我们可以直接在IDEA环境上使用便捷式的内存数据库H2,关于H2更多知识就自己去找 ...

  6. 漏洞复现-CVE-2017-4971-Spring Web Flow 远程代码执行

            0x00 实验环境 攻击机:Win 10 靶机也可作为攻击机:Ubuntu18 (docker搭建的vulhub靶场)(兼顾反弹shell的攻击机) 0x01 影响版本 Spring ...

  7. Java标识符,关键字,保留字三者区分(主要是从官网摘抄)

    区分 标识符(Identifiers) An identifier is an unlimited-length sequence of Java letters and Java digits, t ...

  8. python-顺序队列的实现

    class seqqueue(object): 8 def __init__(self,maxsize): 9 self.maxsize = maxsize 10 self.queueelem = [ ...

  9. 欢迎参加3月活动:AWS 在线研讨会与阿里云 RISC-V 应用创新大赛

    3月份我们在帮合作云厂商 Amazon Web Services(AWS) 与阿里云推广2个活动,欢迎感兴趣的园友参加. 活动一:亚马逊云科技在线研讨会:借助 DGL 实现实时欺诈检测 博客园专属报名 ...

  10. 10 分钟轻松学会 Jackson 反序列化自动适配子类

    作者:丁仪 来源:https://chengxuzhixin.com/blog/post/Jackson-fan-xu-lie-hua-zi-dong-shi-pei-zi-lei.html json ...