1090 Highest Price in Supply Chain (25分)Java与C++两种方法对比

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. 用类似并查集的方法,用一个int类型数组来表示,数组下标表示第几个点,数组值表示该点的父亲是谁。
  2. 定义一个f函数,该函数的作用是输出该点经过几次查找才找到根结点。也就是货物经手了几次
  3. 将每一个点放入,找到f输出最大的值和次数
import java.util.Scanner;

public class P3 {
    static Scanner sc=new Scanner(System.in);
    static int n=sc.nextInt();
    static double p=sc.nextDouble();
    static double r=sc.nextDouble();
    static int[] arr=new int[n];
    public static void main(String[] args) {

        for (int i = 0; i <n ; i++) {
            int m=sc.nextInt();
            if(m==-1) m=i;
            arr[i]=m;
        }
        int max=0;
        int num=0;
        int g;
        for (int i = 0; i <n; i++) {
             g=f(i);
            if(g>max){
                max=g;
                num=0;
            }
            if(g==max){
                num++;
            }
        }
        double o=(100+r)/100;
        p=p*Math.pow(o,max);
        System.out.print(String.format("%.2f",p)+" "+num);
        sc.close();
    }
    static  int f(int i){
        int p=0;
        while(arr[i]!=i){
            i=arr[i];
            p++;
        }
        return p;
    }
}

然后结局就是超时。。。。。。。。。
在这里插入图片描述

二刷思路

  1. 用DFS,深度搜索,比起第一种方法好太多,只需一次遍历就可以找到最大深度和个数
import java.util.ArrayList;
import java.util.Scanner;

public class P1090 {
    static int maxdep=0;
    static int num=0;
    static Scanner sc=new Scanner(System.in);
    static int n=sc.nextInt();
   static ArrayList<Integer>[] lists=new ArrayList[n];
   static double p=sc.nextDouble();
   static double r=sc.nextDouble();
    public static void main(String[] args) {
        for (int i = 0; i <n ; i++) {
            lists[i]=new ArrayList<>();
        }
        int root = 0;
        for (int i = 0; i <n ; i++) {
            int h=sc.nextInt();
            if(h!=-1)
            {lists[h].add(i);}else {
                root=i;
            }
        }
        dfs(root,0);
        double f=p*Math.pow((100+r)/100,maxdep);
        System.out.print(String.format("%.2f",f)+" "+num);
    }
    public static void dfs(int root,int depth){
        if(lists[root].size()==0){
        if(depth>maxdep){
            maxdep=depth;
            num=1;
        }else if(depth==maxdep){
            num++;
        }
        return;
        }
        for (Integer w:lists[root]) {
            dfs(w,depth+1);
        }
    }
}

在这里插入图片描述
总以为这次不会超时,结果还是超时了,有点怀疑人生,于是配置了visual Studio code 里面的C++环境,我怀疑是语言问题吧,将思路二用C++实现

思路2的C++实现

#include<iostream>
using namespace std;
#include<vector>
#include<math.h>
#include <iomanip>
static int maxdep=0;
static int num=0; 
vector<int> lists[10000]; 
void dfs(int root,int depth){
    if(lists[root].size()==0){
        if(depth>maxdep){
            maxdep=depth;
            num=1;
        }else if(depth==maxdep){
            num++;
        }
        return;
    }
        for (int i=0;i<lists[root].size();i++) {
            dfs(lists[root][i],depth+1);
        }
}
int main()
{    int n;cin>>n;
    static double p;
    static double r;
    cin>>p>>r;
    int root = 0;
        for (int i = 0; i <n ; i++) {
            int h;
            cin>>h;
            if(h!=-1)
            {lists[h].push_back(i);}else {
                root=i;
            }
        }
        dfs(root,0);
        double f=p*pow((100+r)/100,maxdep);
        cout<<setiosflags(ios::fixed)<<setprecision(2);
	    cout<<f;
        cout<<" ";
        cout<<num;
        return 0;
}

在这里插入图片描述
这下终于对了,好开心怎么回事,用Java刷PAT面临语言与算法的双重打击啊!
可惜三天后就要PAT考试咯,才发现c++的好,哭了。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值