1090 Highest Price in Supply Chain——PAT甲级真题
1090 Highest Price in Supply Chain
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 (<=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.
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.
Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6Sample Output:
1.85 2
题目大意:题目大致意思与1079 Total Sales of Supply Chain 相同,无非就是所求结果相同,这道题让你输出可以卖出的最高价格和能卖出最高价格的销售商的人数。
大致思路:利用DFS从根节点往下搜索,记录每个叶子结点的编号方便后续统计价格相同的个数。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
struct node {
vector<int> child;
double sell;
}root[N];
double p, r;
int n;
vector<double> cost;
vector<int> leaf;
double ans = 0.0;
void newNode (int x) {
root[x].sell = 0.0;
root[x].child.clear();
}
void DFS(int x) {
if (root[x].child.size() == 0) {
ans = max(ans, root[x].sell);
leaf.push_back(x);
return;
}
for (int i = 0; i < root[x].child.size(); i++) {
int tmp = root[x].child[i];
root[tmp].sell = (1 + 0.01 * r) * root[x].sell;
DFS(tmp);
}
}
int main() {
scanf("%d%lf%lf", &n, &p, &r);
int root_index;
for (int i = 0; i < n; i++) newNode(i);
for (int i = 0; i < n; i++) {
int s;
scanf("%d",&s);
if (s == -1) {
root_index = i;
continue;
}
root[s].child.push_back(i);
}
root[root_index].sell = p;
DFS(root_index);
int cnt = 0;
for (auto l : leaf) {
if (root[l].sell == ans) cnt++;
}
printf("%.2lf %d\n", ans, cnt);
return 0;
}
最新文章
- RSA的傻瓜原理
- 【原】iOS动态性(二):运行时runtime初探(强制获取并修改私有变量,强制增加及修改私有方法等)
- js-JavaScript高级程序设计学习笔记1
- OC-copy
- Android runProguard配置 导致module lib 中的包编译时无法识别
- MVP Community Camp 社区大课堂
- Mac OS 上设置 JAVA_HOME
- onsubmit提交前先验证(验证不通过阻止form提交)
- Working with Numbers in PL/SQL(在PL/SQL中使用数字)
- svn问题(队列)
- iOS 沙盒
- Scala学习笔记(二)
- 你知道如何为iOS工程改名吗?
- git add 添加多个文件
- HDU 1049(蠕虫爬井 **)
- SpringBoot 注解
- Java设计模拟菜单
- IE浏览器调试工具不能使用
- 测试sql server服务是否配置正确
- ubuntu18.04配置nvidia docker和远程连接ssh+远程桌面连接(二)