陪我一起刷PAT甲级系列--Highest Price in Supply Chain

Posted C_YCBX Py_YYDS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了陪我一起刷PAT甲级系列--Highest Price in Supply Chain相关的知识,希望对你有一定的参考价值。


更多精彩尽在我的个人小站: acking-you.github.io

题目

题目翻译

单词积累

involved in 卷入
distribute 分配、分散、分发
retailers 零售商
#例句
everyone involved in moving a product from supplier to customer.
#翻译
所有人卷入把产品从供应商到顾客的过程。

题目描述

原文
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.
翻译
有一个由零售商、经销商、供应商组成的供应链网络,所有人都参与把产品从供应商到顾客的过程。
从一个根供销商开始,在供应链上的所有人都要从供应商以价格P买下产品,并以高于P r%的价格销售或分发它们。
假设每一个成员在供应链都有一个确切的供应商,除了根供应商,并且没有供应链死循环。
现在给定一个供应链,你应该求出这些零售商中最贵的商品价格。

输入描述

原文
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. S_root for the root supplier is defined to be -1. All the numbers in a line are separated by a space.
翻译
每个测试用例,第一行包括三个正整数:N(<=105),在供应链中的成员数量(并且被标记为0~N-1);P,给定的根供应链的 价格; r,每次售卖或者分发的百分率价格增量;接下来一行有N个数字每个数字 Si 代表 i 的供应商是谁。root供应商的的值被定义为-1.这一行所有的数字用一个空格隔开。

输出描述

原文
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.
翻译
打印一行:1. 从经销商手里能够拿到的最高价格,精确到小数点后两位。2. 以及售卖这样高价格的经销商数量。
保证价格不会超过10^10。

题目解析

仔细把题目一阅读发现,马上就应该有思路了,照着每个点去深度遍历即可,中间不断的乘以百分率,这个中间结果,可以用路径压缩的思想来优化保存下来(有并查集那味了)。而最大值的经销商数量,可以用之前Dijkstra算法的那几题的思路来写,一旦没有达到最大值(还在更新,则数量不断的给他清为1),出现==情况,则 +1。

代码详解

基本的全局数据

int chain[MaxN]; //数据链
double prices[MaxN]; //路径压缩的价格保存
int N;		//有多少个经销商
double rootP,r; //根价格和 对应的百分率分子
int root;	//根供应商
double percent_r; //对应的百分率

Input函数

// 负责数据的输入和初始化
void Input(){
    ios::sync_with_stdio(false);
    memset(prices,0,sizeof(prices));
    cin>>N>>rootP>>r;
    percent_r = r/100.0;
    for(int i=0;i<N;i++){
        cin>>chain[i];
        if(chain[i]==-1)
            root = i;
    }
}

dfs函数

//用于路径压缩和递归求值
double dfs(int i){
    if(i==root)
        return rootP;
    if(prices[i])
        return prices[i];
    double tt = dfs(chain[i]);
    tt += tt*percent_r;
    return prices[i] = tt;
}

print函数

//打印结果,注意保留两位小数
void print(){
    double res = 0;
    int nums = 0;
    for(int i=0;i<N;i++){
        double t = dfs(i);
        if(res<t){
            res = t;
            nums = 1;
        }
        else if(res==t){
            nums++;
        }
    }
    cout<<fixed<<setprecision(2)<<res<<' '<<nums;
}

整合代码得答案

效率还行

#include <bits/stdc++.h>
#define MaxN 100002
using namespace std;

int chain[MaxN];
double prices[MaxN];
int N;
double rootP, r;
int root;
double percent_r;

void Input() {
    ios::sync_with_stdio(false);
    memset(prices, 0, sizeof(prices));
    cin >> N >> rootP >> r;
    percent_r = r / 100.0;
    for (int i = 0; i < N; i++) {
        cin >> chain[i];
        if (chain[i] == -1)
            root = i;
    }
}

double dfs(int i) {
    if (i == root)
        return rootP;
    if (prices[i])
        return prices[i];
    double tt = dfs(chain[i]);
    tt += tt * percent_r;
    return prices[i] = tt;
}

void print() {
    double res = 0;
    int nums = 0;
    for (int i = 0; i < N; i++) {
        double t = dfs(i);
        if (res < t) {
            res = t;
            nums = 1;
        } else if (res == t) {
            nums++;
        }
    }
    cout << fixed << setprecision(2) << res << ' ' << nums;
}

int main() {
    Input();
    print();
    return 0;
}

以上是关于陪我一起刷PAT甲级系列--Highest Price in Supply Chain的主要内容,如果未能解决你的问题,请参考以下文章

PAT甲级刷题实录——1014

PAT甲级刷题实录——1013

PAT甲级刷题实录——1004

PAT甲级刷题实录——1010

(刷算法学英语)PAT甲级--Acute Stroke(三维空间的bfs)

PAT 甲级 A1070 (2019/02/19)