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

題目大意:給出一個數組a[i] = j; 表示i的供應商是j, 當a[i]=-1的時候,表示i是最頂端的供應商。求供應鏈的最長長度,以及處于最長長度供應鏈末端零售商的人數
對給出的數據做以下處理:建立一個二維vector向量,chain[i]表示i為供應商,chain[i][j]表示j的供應商是i。chain[i].size()=0 則表示i是零售商
            maxdepth記錄供應鏈的最長長度,cnt記錄處于最長供應鏈末端零售商的人數。
這一題其實就是用數組建立樹,并遍歷樹的過程。從頂端供應商root開始遍歷,直到遍歷到零售商為止(即chain[i].size()=0); 遍歷過程中,如果發現當前的depth比maxdepth大,則更新maxd,并更新cnt=1;
若遍歷過程中發現當前depth=maxdepth則更新cnt的值;

注意點:
  計算最高價格的時候,要用double類型, 用float類型,會有一些測試點不能通過,應該是溢出導致
  在dfs()遍歷樹的時候,不能調用dfs(index, depth++), 應該用dfs(index, depth+1); 前者會改變同意層次循環中的depth值導致最終結果錯誤


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int cnt=0, maxdepth=0;
double ans=0.0;
vector<vector<int>> chain;
void dfs(int index, int depth){if(chain[index].size()==0){if(depth>maxdepth){maxdepth=depth; cnt=1;}else if(depth==maxdepth) cnt++;return;}for(int i=0; i<chain[index].size(); i++) dfs(chain[index][i], depth+1);
}
int main(){int n, i, root, temp;double p, r;cin>>n>>p>>r;chain.resize(n);for(i=0; i<n; i++){cin>>temp;if(temp==-1){root=i; continue;}chain[temp].push_back(i);}dfs(root, 0);printf("%.2f %d", p*pow((1.0+r/100.0), maxdepth), cnt);return 0;
}

?

轉載于:https://www.cnblogs.com/mr-stn/p/9139221.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/397761.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/397761.shtml
英文地址,請注明出處:http://en.pswp.cn/news/397761.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mysql 列數據顯示轉成行數據顯示_Mysql的列修改成行并顯示數據的簡單實現

創建測試表&#xff1a;DROP TABLE IF EXISTS test;CREATE TABLE test (year int(11) DEFAULT NULL,month int(11) DEFAULT NULL,amount double DEFAULT NULL) ENGINEInnoDB DEFAULT CHARSETutf8;插入數據&#xff1a;INSERT INTO test VALUES (1991, 1, 1.1);INSERT INTO test…

Android兩種常見錯誤(ANR和FC)解決辦法

ANR(Activity Not Respone)(無響應)先介紹下Main線程&#xff08;也稱為UI線程、主線程&#xff09;功能: 1.創建UI控件2.更新UI控件狀態3.事件處理限制&#xff1a;Main線程不建議有超過5秒的事件出現條件&#xff1a;當用戶輸入事件5s內沒有得到響應&#xff0c;將彈出ANR對話…

mysql命令(command)

連接mysql命令: mysql -h 192.168.1.1 -P 3306 -uuserName -pPassword 顯示表的索引: SHOW INDDEX FROM table_name 查看mysql的超時時間&#xff1a;SHOW GLOBAL VARIABLES LIKE %timeout% 備份表結構和表數據&#xff1a;mysqldump -u用戶名 -p 庫名 表1 表2 > xxx.sql只…

微信5.0登錄提示服務器繁忙,iOS集成友盟社會化分享微信無法登錄?

iOS集成友盟社會化分享SDK-5.0點擊微信登錄的時候出現無法獲取accessToken的現象&#xff0c;其他如QQ、微博都可以正常登錄使用。另外QQ、微博和微信分享都可以正常使用。望各位早日幫我解決或者分析一下。謝謝//微信登錄之后的回調- (BOOL)application:(UIApplication *)appl…

sql獲取某列出現頻次最多的值_業務硬核SQL集錦

戳上方藍字關注我 這兩年學會了跑sql&#xff0c;當時有很多同學幫助我精進了這個技能&#xff0c;現在也寫成一個小教程&#xff0c;反饋給大家。適用對象&#xff1a;工作中能接觸到sql查詢平臺的業務同學(例如有數據查詢權限的產品與運營同學)適用場景&#xff1a;查詢hive&…

void ,NULL與0的區別聯系

void ,NULL及0的區別聯系 void的詳解: void的字面意思是“無類型”或“空類型”&#xff0c;void*則為“無針型指針”&#xff0c;那就意味著void*可以指向任何類型的數據。 眾所周知&#xff0c;如果指針p1和p2的類型相同&#xff0c;那么我們可以直接在p1和p2間互相賦值&…

python 2 days

1&#xff0c;格式化輸出&#xff0c;%s %d 2&#xff0c;復習昨日講題 編譯型&#xff1a; 將代碼一次性全部編譯成二進制&#xff0c;然后運行。 優點&#xff1a;執行效率高。 缺點&#xff1a;開發效率低&#xff0c;不能跨平臺。 C解釋型&#xff1a; 代碼…

nginx編譯安裝與配置使用

第一部分----nginx基本應用源碼編譯安裝nginx1、安裝pcre軟件包&#xff08;使nginx支持http rewrite模塊&#xff09;yum install -y pcre yum install -y pcre-devel2、安裝openssl-devel&#xff08;使nginx支持ssl&#xff09;yum install -y openssl-devel3、創建用戶ngin…

ubuntu+查看服務器文件夾權限,Ubuntu - 文件夾權限查看與修改

Ubuntu 文件的歸屬身份有四種&#xff1a;u - 擁有文件的用戶(所有者)g - 所有者所在的組群o - 其他人(不是所有者或所有者的組群)a - 每個人或全部(u, g, o)1. 查看文件/文件夾權限ls -l filename # 查看文件權限ls -ld folder # 查看文件夾權限輸出結果如&#xff1a;drwxrwx…

mysql dump 1449_跨版本mysqldump恢復報錯Errno1449

已經有一套主從mysql,新增兩個slave主庫Server version: 5.6.22-log MySQL Community Server (GPL)舊從庫Server version: 5.6.28-log MySQL Community Server (GPL)新增SLAVE 1&#xff1a; Server version: 5.6.22-log MySQL Community Server (GPL)新增SLAVE 2&#xff1a; …

修復 Xcode 錯誤 “The identity used to sign the executable is no longer valid”

如圖&#xff1a; 解決方法來自&#xff1a;http://stackoverflow.com/questions/7088441/the-identity-used-to-sign-the-executable-is-no-longer-valid/14275197 Restarting Xcode didnt work for me. What fixed it for me was going to Accounts in Xcode (in preferences…

centos設置ip

這里是centos7.vmware安裝centos后需要設置ip 1.首先查看虛擬機的網絡適配器信息 2.根據信息修改配置文件 vi /etc/sysconfig/network-scripts/ifcfg-ens33 圖為修改后的,最初的配置為 BOOTPROTOdhcp ONBOOTno IPADDR,GATEWAY,NETMASK沒有進行配置需要根據網絡適配器配置手動維…

微信支付+服務器+php代碼,php 微信支付企業付款(示例代碼)

/*** 格式化參數格式化成url參數*/public function ToUrl($arr){$buff "";foreach ($arr as $k > $v){if($k ! "sign" && $v ! "" && !is_array($v)){$buff . $k . "" . $v . "&";}}$buff trim($b…

Spark踩坑記——數據庫(Hbase+Mysql)轉

轉自&#xff1a;http://www.cnblogs.com/xlturing/p/spark.html 前言 在使用Spark Streaming的過程中對于計算產生結果的進行持久化時&#xff0c;我們往往需要操作數據庫&#xff0c;去統計或者改變一些值。最近一個實時消費者處理任務&#xff0c;在使用spark streaming進行…

解決Failed to connect session for conifg 故障

服務器升級openssh之后jenkins構建報錯了&#xff0c;報錯信息如下&#xff1a; Failed to connet or change directory jenkins.plugins.publish_over.BapPublisherException:Failed to connect session for config.....Message [Algorithm negotiation fail] 升級前ssh版本&a…

78oa mysql_78oa系統版本升級方法

可升級版本預覽升級方法&#xff1a;1、備份數據庫、附件目錄、二次開發程序打開開始菜單——控制面板——管理工具——服務&#xff0c;右鍵點擊停止 78oa mysql service 服務&#xff0c;完整復制【D:\78OA\server\modules\storage\data\78oa】(數據庫)文件夾至備份區域。完整…

Excel導出顯示服務器意外,C# 調用Excel 出現服務器出現意外狀況. (異常來自 HRESULT:0x80010105 (RPC_E_SERVERFAULT)...

C# 調用Excel 出現服務器出現意外狀況. (異常來自 HRESULT:0x80010105 (RPC_E_SERVERFAULT)htmlprivate Microsoft.Office.Interop.Excel.Application xApp;private Microsoft.Office.Interop.Excel.Workbook xBook;服務器//變量xApp new Microsoft.Office.Interop.Excel.Appl…

列表、元組、字典、集合的定義、操作與綜合練習

l[A,B,C] t{A,B,C}l.append(B)print(l)scores[66,77,88]d{A:66,B:77,C:88} d[B]99 d[D]111 d.pop(C) print(d)s1{A,B,C} s2{A,C,D} print(s1&s2) print(s1|s2) 轉載于:https://www.cnblogs.com/chenjunyu666/p/9147417.html

xargs

find /tmp/ -name "*.log" -mtime 4 | xargs -i -t mv {} /home/ find /tmp/ -name "*.log" -mtime 4 -print0 | xargs -0 rm -f xargs(1) xargs是給命令傳遞參數的一個過濾器&#xff0c;也是組合多個命令的一個工具。它把一個數據流分割為一些足夠小的塊…

export mysql home_mysql的Linux下安裝筆記

注&#xff1a;在5.7之后MySQL不在生成my-default.cnf配置。tar -xzvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gzmv mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz/ /usr/local/mysql新建 useradd mysql新建文件夾mkdir /usr/local/mysql/data生成配置&#xff1a;./mysqld -…