当前位置:网站首页>PAT tree DP (memory search) class a, 1079, 1090, 1106
PAT tree DP (memory search) class a, 1079, 1090, 1106
2022-08-02 17:02:00 【keyboard sonata】
树状dp:Used for any node in the tree to the root node distance
模板:
int dfs(int i){
if(f[i] != -1) return f[i];
if(p[i] == -1) return f[i] = 0;
return f[i] = dfs(p[i]) + 1;
}
原题链接:
题目:
供应链总销售额
Supply chain is made up of retailers,Dealers and suppliers of sales network,Everyone involved in moving products from suppliers to customers.
The whole sales network can be viewed as a tree structure,From the roots of suppliers,Each person to buy goods from vendors at the next higher level after,Assume that buying price for P,Will bid higher than that of in r% The price down to sell.
Only the retailer(即叶节点)Can directly to sell the products to the customer.
现在,Given the whole sales network,Would you please calculate all retailers total sales.
输入格式
The first line contains three number,N Said the supply chain to the total number of members(All the members of the Numbers from 0 到 N−1,The roots of supplier Numbers for 0),P Said the roots of suppliers of each product's selling price,r,Premium percentage.接下来 N 行,Each row contains a member information,格式如下:
Ki ID[1] ID[2] … ID[Ki]
其中第 i 行,Ki Said from the supplier i A member of the direct purchase number,接下来 Ki An integer is the serial number of members of each stock.如果某一行的 Kj 为 0,Said this is retailers,Then later only will follow a number,Said to sell to customer product total number.
输出格式
Output total sales,保留一位小数.数据范围
1≤N≤105,
0<P≤1000,
0<r≤50
Each retailer in the hands of the product is not more than 100 件.
Ultimate answer to ensure that no more than 1010.输入样例:
10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3
输出样例:
42.4
我的解法:
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
double R, P;
int p[N], f[N], cnt[N];
int dfs(int i){
if(f[i] != -1) return f[i];
if(p[i] == -1) return f[i] = 0;
return f[i] = dfs(p[i]) + 1;
}
int main(){
int n;
cin >> n >> P >> R;
memset(f, -1, sizeof f);
memset(p, -1, sizeof p);
for(int i = 0; i < n; i ++ ){
int k;
cin >> k;
for(int j = 0; j < k; j ++ ){
int son;
cin >> son;
p[son] = i;
}
if(k == 0) cin >> cnt[i];
}
double res = 0;
for(int i = 0; i < n; i ++ ){
if(cnt[i]){
res += cnt[i] * P * pow(1 + R/100, dfs(i));
}
}
printf("%.1lf", res);
return 0;
}
供应链最高价格
Supply chain is made up of retailers,Dealers and suppliers of sales network,Everyone involved in moving products from suppliers to customers.
The whole sales network can be viewed as a tree structure,From the roots of suppliers,Each person to buy goods from vendors at the next higher level after,Assume that buying price for P,Will bid higher than that of in r% The price down to sell.
Only the retailer(即叶节点)Can directly to sell the products to the customer.
现在,Given the whole sales network,Would you please calculate retailers can achieve the highest selling price.
输入格式
The first line contains three number,N Said the supply chain to the total number of members(All the members of the Numbers for 0 到 N−1);P Said the roots of supplier product sales price;r,Said percentage premium.第二行包含 N 个数字,第 i 个数字 Si 是编号为 i The serial number of the members of the superior supplier.The roots of suppliers Sroot 为 -1.
输出格式
Output retailers can achieve the highest selling price,保留两位小数,The number of retailers and can achieve the highest selling price.数据范围
1≤N≤105,
0<P≤1000,
0<r≤50,
Ultimate answer to ensure that no more than 1010.输入样例:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
输出样例:
1.85 2
我的解法:
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n;
double P, R;
int p[N], f[N];
int dfs(int i){
if(f[i] != -1) return f[i];
if(p[i] == -1) return f[i] = 0;
return f[i] = dfs(p[i]) + 1;
}
int main(){
cin >> n >> P >> R;
memset(p, -1, sizeof p);
for(int i = 0; i < n; i ++ ){
int father;
cin >> father;
p[i] = father;
}
memset(f, -1, sizeof f);
int cnt = 0, res = 0;
for(int i = 0; i < n; i ++ ){
if(dfs(i) > res){
res = dfs(i);
cnt = 1;
}
else if(dfs(i) == res) cnt ++;
}
printf("%.2lf %d", P * pow(1 + R / 100, res), cnt);
return 0;
}
供应链最低价格
Supply chain is made up of retailers,Dealers and suppliers of sales network,Everyone involved in moving products from suppliers to customers.
The whole sales network can be viewed as a tree structure,From the roots of suppliers,Each person to buy goods from vendors at the next higher level after,Assume that buying price for P,Will bid higher than that of in r% The price down to sell.
Only the retailer(即叶节点)Can directly to sell the products to the customer.
现在,Given the whole sales network,Would you please calculate retailers can achieve the lowest selling price.
输入格式
The first line contains three number,N Said the supply chain to the total number of members(All the members of the Numbers from 0 到 N−1,The roots of supplier Numbers for 0),P Said the roots of supplier product selling price,r,Premium percentage.接下来 N 行,Each row contains a member information,格式如下:
Ki ID[1] ID[2] … ID[Ki]
其中第 i 行,Ki Said from the supplier i A member of the direct purchase number,接下来 Ki An integer is the serial number of members of each stock.如果 Kj 为 0 则表示第 j Members of the team is a retailer.
输出格式
Output retailers can achieve the lowest selling price,保留四位小数,The number of retailers and can reach the lowest selling price.数据范围
1≤N≤105,
0<P≤1000,
0<r≤50,
Ultimate answer to ensure that no more than 1010
输入样例:
10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0
2 6 1
1 8
0
0
0
输出样例:
1.8362 2
我的解法:
#include <bits/stdc++.h>
using namespace std;
const int N = 100010, INF = 1e9;
int n;
double P, R;
int p[N], f[N];
bool is_leaf[N];
int dfs(int i){
if(f[i] != -1) return f[i];
if(p[i] == -1) return f[i] = 0;
return f[i] = dfs(p[i]) + 1;
}
int main(){
cin.tie();
cin >> n >> P >> R;
memset(p, -1, sizeof p);
for(int i = 0; i < n; i ++ ){
int k;
cin >> k;
for(int j = 0; j < k; j ++ ){
int son;
cin >> son;
p[son] = i;
}
if(k == 0) is_leaf[i] = true;
}
memset(f, -1, sizeof f);
int res = INF, cnt = 0;
for(int i = 0; i < n; i ++ ){
if(is_leaf[i]){
if(dfs(i) < res){
res = dfs(i);
cnt = 1;
}
else if(dfs(i) == res) cnt ++ ;
}
}
printf("%.4lf %d", P * pow(1 + R / 100, res), cnt);
return 0;
}
边栏推荐
- Servlet 技术2
- 2022-07-16 第五小组 瞒春 学习笔记
- ADB常用命令--测试人员必备
- 已解决ModuleNotFoundError: No module named‘ pip‘(重新安装pip的两种方式)
- Principles of permutation entropy, fuzzy entropy, approximate entropy, sample entropy and approximate entropy implemented by MATLAB
- 状态码以及访问百度过程
- 事件对象,事件流(事件冒泡和事件捕获)、事件委托、L0和L2注册等相关概念及用法
- 2022-02-14 第五小组 瞒春 学习笔记
- 2021年华为杯数学建模竞赛E题——信号干扰下的超宽带(UWB)精确定位问题
- 什么是Nacos?
猜你喜欢
DOM - Event Delegate
2022年低压电工考试试题及在线模拟考试
第五章-5.2-指示器随机变量
Based on the SVM regression forecast 】 【 LibSVM realize the prediction of a characteristic data
什么是Nacos?
web渗透之文件上传漏洞
面试了个阿里P7大佬,他让我见识到什么才是“精通高并发与调优”
[Time series model] AR model (principle analysis + MATLAB code)
static关键字的三种重要作用详解
为什么四个字节的float表示的范围比八个字节的long要广
随机推荐
常见(MySQL)面试题(含答案)
2022-07-25 第六小组 瞒春 学习笔记
CSV file with the data read and write 】 【 XLS/XLSX file
2022-7-12 第五组 瞒春 学习报告
lammps聚合物建模——EMC
PAT甲级 1130 中缀表达式
JS中的数组方法和循环
加点字符就能让qq昵称很酷的神奇代码?
事件对象,事件流(事件冒泡和事件捕获)、事件委托、L0和L2注册等相关概念及用法
炎炎夏日打造一个属于自己的“便携小空调”吧
2022-07-19 第五小组 瞒春 学习笔记
MATLAB中dist与pdist、pdist2的区别与联系
PAT甲级 1145 哈希 - 平均查找时间
只出现一次的数字||| —— 哈希映射、异或位运算+分治思想
为什么float4个字节比long8个字节所表示的数值范围广
ELK日志分析系统
职工管理系统(SSM整合)
VsCode更新后,怎么使用使用快捷键同时生成多个元素
如何正确且快速的清楚C盘!!释放C盘空间内存保姆级教程
如何查看微信小程序服务器域名并且修改