PAT甲级 -- 1090 Highest Price in Supply Chain (25 分)

本文介绍了一种计算供应链中从供应商到零售商的产品最高预期价格的方法。通过构建一个包含零售商、分销商和供应商的网络,每个成员根据一定的百分比加价率从其供应商购买产品,最终确定最高零售价格及达到此价格的零售商数量。

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 (≤10​5​​), 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 −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 10​10​​.

Sample Input:

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

Sample Output:

1.85 2

思路:

1. 与和1106题是一样的,不过这个题求最高价格。https://blog.youkuaiyun.com/hhuzxx/article/details/90775591

2. 输入也有点不一样,直接拿原来的代码改动一下就可以啦~

#include <iostream>
#include <vector>
using namespace std;
struct node{
	double price;
	vector<int> child;
};
vector<node> supply;
int N;
double r; //N个chain上的人,r%的比率
double P; //P为最初的价格
double maxPrice = -1;
int cnt = 0;
void dfs(int root, double p){
	supply[root].price = p;
	if(supply[root].child.size()== 0){  //到达叶子结点
		if(maxPrice < p){
			cnt = 1;
			maxPrice = p;
		}else if(maxPrice == p){
			cnt++;
		}
	}
	for (int i = 0; i < supply[root].child.size(); i++){
		dfs(supply[root].child[i], p * (100+r)/100.0);
	}
}
int main(){
	scanf("%d %lf %lf", &N, &P, &r);
	supply.resize(N);
	int root = 0;
	for (int i = 0; i < N; i++){
		int k;
		scanf("%d", &k);
		if(k == -1)root = i;
		else supply[k].child.push_back(i);		
	}
	dfs(root,P);
	printf("%.2f %d", maxPrice, cnt);
	return 0;
}

 

### PAT 乙级 1004 成绩排名题目解析 #### 题目描述 该题目要求读取 `n` 名学生的信息,每条记录包含学生的姓名、学号以及成绩。目标是找出并输出成绩最高和最低的学生的姓名及其对应的学号。 输入数据的第一行为一个正整数 `n`,表示参与评估的学生总数;随后的每一行则依次给出每位同学的具体资料——名字、编号与数[^2]。需要注意的是,这里的名称长度不会超过十个字母,并且所有人的得都在零至一百之间不重复存在[^3]。 #### 解决方案概述 为了实现上述功能,可以采用如下策略: - 创建结构体来存储单个考生的数据; - 使用数组保存全部学员信息以便后续处理; - 设定变量追踪最大最小值连同对应位置索引; - 最终遍历整个列表找到最优解后打印结果即可。 下面是具体的 C++ 实现方法: ```cpp #include <iostream> #include <vector> using namespace std; struct Student { string name; string id; int score; }; int main() { vector<Student> students; int n, maxScore = -1, minScore = 101, maxIndex = 0, minIndex = 0; cin >> n; for(int i=0;i<n;++i){ Student temp; cin>>temp.name>>temp.id>>temp.score; if(temp.score > maxScore){ maxScore=temp.score; maxIndex=i; } if(temp.score<minScore){ minScore=temp.score; minIndex=i; } students.push_back(temp); } cout << "Highest Score:\nName: "<<students[maxIndex].name<<"\tID:"<<students[maxIndex].id<<endl; cout << "\nLowest Score:\nName: "<<students[minIndex].name<<"\tID:"<<students[minIndex].id<<endl; } ``` 此程序首先定义了一个名为 `Student` 的结构用来封装个人详情。接着通过循环接收用户输入并将之存入向量容器之中同时更新极值标记直至结束为止。最后按照指定格式显示最终答案。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值