1090. Highest Price in Supply Chain (25)【树】——PAT (Advanced Level) Practise

本文介绍了一种计算供应链中零售商能期望的最高价格的方法。通过构建树形结构并进行搜索,确定从根供应商到最终零售商的价格增长过程。输入包含供应链成员数量、初始价格及价格增长率。

题目信息

1090. Highest Price in Supply Chain (25)

时间限制200 ms
内存限制65536 kB
代码长度限制16000 B
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 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 6
Sample Output:
1.85 2

解题思路

建树搜索

AC代码

#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
int fa[100005];
vector<int> sroot, child[100005];
int maxLevel = -1, maxNum;

void dfs(int root, int lv){
    if (lv > maxLevel){
        maxLevel = lv;
        maxNum = 1;
    }else if (lv == maxLevel){
        ++maxNum;
    }
    for (int i = 0; i < child[root].size(); ++i){
        dfs(child[root][i], lv + 1);
    }
}
int main()
{
    int n, t;
    double p, r;
    scanf("%d%lf%lf", &n, &p, &r);
    for (int i = 0; i < n; ++i){
        scanf("%d", &t);
        fa[i] = t;
        if (t == -1){
            sroot.push_back(i);
        }else{
            child[t].push_back(i);
        }
    }
    int mx = 0;
    for (int i = 0; i < sroot.size(); ++i){
        dfs(sroot[i], 0);
    }
    printf("%.2f %d\n", p * pow(1+r/100, maxLevel), maxNum);
    return 0;
}
考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
### 含义 `emp.salary BETWEEN j.lowest_sal AND j.highest_sal` 是一个条件判断语句,用于筛选出满足特定范围条件的数据。`emp.salary` 表示员工表(`emp`)中的工资字段,`j.lowest_sal` 和 `j.highest_sal` 分别表示另一个表(`j`)中的最低薪资和最高薪资字段。该语句会筛选出 `emp` 表中工资处于 `j` 表对应记录的最低薪资和最高薪资之间(包含边界值)的员工记录。例如,若 `j.lowest_sal` 为 2000,`j.highest_sal` 为 5000,那么 `emp.salary` 为 2000、3000、5000 的记录都会被筛选出来,而 1500 和 5500 则不会被筛选出来 [^1][^2]。 ### 使用场景 - **薪资分级查询**:如查询出不同薪资等级的员工信息。像在引用[1]中,结合 `jg.grade_level = "A"` 条件,查询出工资级别为 A 的员工的工资和工资级别。 - **数据范围筛选**:当需要根据某个字段的范围进行数据筛选时,都可以使用该语句。例如在人力资源管理系统中,筛选出工作年限在某个区间的员工,或者在财务管理系统中,筛选出金额在一定范围内的交易记录。 ### 优化 - **索引优化**:为 `emp.salary`、`j.lowest_sal` 和 `j.highest_sal` 字段创建索引。索引可以加快数据的查找速度,尤其是在处理大量数据时。例如,在 MySQL 中可以使用以下语句创建索引: ```sql CREATE INDEX idx_emp_salary ON emp(salary); CREATE INDEX idx_j_lowest_sal ON j(lowest_sal); CREATE INDEX idx_j_highest_sal ON j(highest_sal); ``` - **避免函数操作**:尽量避免在 `BETWEEN` 条件的字段上使用函数操作,因为函数操作会使索引失效。例如,不要写成 `emp.salary + 100 BETWEEN j.lowest_sal AND j.highest_sal`。 - **分区表**:如果数据量非常大,可以考虑使用分区表。例如,按照薪资范围对 `emp` 表进行分区,这样在查询时可以减少扫描的数据量。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值