PAT中的树(下):

本文档介绍了四个与供应链管理相关的编程问题,包括计算总销售额(PAT1079)、查找最高价格(PAT1090)、最低价格(PAT1106),以及验证路径性质(PAT1155)。通过递归和动态规划实现算法,涉及商品定价策略和树形结构分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PAT1079:供应链总销售额

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
const int N = 100010;

int n;
double P, R;
int p[N], cnt[N], f[N];
int dfs(int u)
{
    if(f[u] != -1) return f[u];
    
    if(p[u] == -1) return f[u] = 0;
    
    return f[u] = dfs(p[u]) + 1;
}
int main()
{
    cin >> n >> P >> R;
    memset(p, -1, sizeof p);
    
    for (int i = 0; i < n; i ++ )
    {
        int k;
        scanf("%d", &k);
        
        for (int j = 0; j < k; j ++ )
        {
            int son;
            scanf("%d", &son);
            p[son] = i;
        }
        
        if(!k) cin >> cnt[i];
    }
    
    memset(f, -1, sizeof f);
    
    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;
}

PAT1090:供应链最高价格

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
const int N = 100010;
int n;
double P, R;
int f[N], p[N];
int dfs(int u)
{
    if(f[u] != -1) return f[u];
    
    if(p[u] == -1) return f[u] = 0;
    
    return f[u] = dfs(p[u]) + 1;
}
int main()
{
    cin >> n >> P >> R;
    memset(p, -1, sizeof p);
    memset(f, -1, sizeof f);
    for (int i = 0; i < n; i ++ )
    {
        int x;
        scanf("%d", &x);
        p[i] = x;
    }
    
    int res = 0, cnt = 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;
}

PAT1106:供应链最低价格

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
const int N = 100010;

int n;
double P, R;
int p[N], f[N];
bool y[N];
int dfs(int u)
{
    if(f[u] != -1) return f[u];
    
    if(p[u] == -1) return f[u] = 0;
    
    return f[u] = dfs(p[u]) + 1;
}
int main()
{
    cin >> n >> P >> R;
    memset(p, -1, sizeof p);
    
    for (int i = 0; i < n; i ++ )
    {
        int k;
        scanf("%d", &k);
        
        for (int j = 0; j < k; j ++ )
        {
            int son;
            scanf("%d", &son);
            p[son] = i;
        }
        
        if(!k) y[i] = true;
    }
    
    memset(f, -1, sizeof f);
    
    int res = 1e9, cnt = 0;
    for (int i = 0; i < n; i ++ )
    {
        if(y[i])
        {
            if(dfs(i) < res)
            {
                res = dfs(i);
                cnt = 1;
            }
            else if(dfs(i) == res) cnt ++;
        }
    }
    printf("%.4lf %d\n", P * pow(1 + R / 100, res), cnt);
    return 0;
}

PAT1155:对路径

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;
const int N = 1010;
int n;
int f[N];
vector<int> path;
bool gt, lt;
void dfs(int u)
{
    path.push_back(f[u]);
    
    if(u * 2 > n)
    {
        cout << path[0];
        for (int i = 1; i < path.size(); i ++ )
        {
            printf(" %d", path[i]);
            if(path[i] > path[i - 1]) gt = true;
            else if(path[i] < path[i - 1]) lt = true;
        }
        puts("");
    }
    
    if(u * 2 + 1 <= n) dfs(u * 2 + 1);
    if(u * 2 <= n) dfs(u * 2);
    path.pop_back();
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i ++ ) cin >> f[i];
    
    dfs(1);
    if(gt && lt) puts("Not Heap");
    else if(gt) puts("Min Heap");
    else puts("Max Heap");
    
    return 0;
}

PAT1130:中缀表达式

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 25;
int n;
int l[N], r[N];
string w[N];
bool st[N], is_leaf[N];
string dfs(int u)
{
    string left, right;
    if(l[u] != -1)
    {
        left = dfs(l[u]);
        if(!is_leaf[l[u]]) left = "(" + left + ")";
    }
    if(r[u] != -1)
    {
        right = dfs(r[u]);
        if(!is_leaf[r[u]]) right = "(" + right + ")";
    }
    
    return left + w[u] + right;
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i ++ )
    {
        cin >> w[i] >> l[i] >> r[i];
        if(l[i]) st[l[i]] = true; // 判断是否时根节点
        if(r[i]) st[r[i]] = true;
        
        if(l[i] == -1 && r[i] == -1) is_leaf[i] = true;
    }
    
    int root = 1;
    while (st[root]) root ++;
    
    cout << dfs(root) << endl;

    return 0;
}

PAT1143:最低公共祖先

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>

using namespace std;
unordered_map<int, int> pos; // 作用是为了建立数值和坐标的映射
const int N = 10010;
int m, n;
int in[N], pre[N], seq[N];
int depth[N], p[N];
int build(int il, int ir, int pl, int pr, int d)
{
    int root = pre[pl];
    int k = root;
    depth[root] = d;
    if(il < k) p[build(il, k - 1, pl + 1, pl + 1 + k - 1 - il, d + 1)] = root;
    if(ir > k) p[build(k + 1, ir, pl + 1 + k - 1 - il + 1, pr, d + 1)] = root;
    
    return root;
}

int main()
{
    cin >> m >> n;
    for (int i = 0; i < n; i ++ )
    {
        scanf("%d", &seq[i]);
        pre[i] = seq[i];
    }
    
    sort(seq, seq + n);
    
    for (int i = 0; i < n; i ++ )
    {
        pos[seq[i]] = i;
        in[i] = i;
    }
    for (int i = 0; i < n; i ++ ) pre[i] = pos[pre[i]];
    
    build(0, n - 1, 0, n - 1, 0);
    
    while (m -- )
    {
        int a, b;
        scanf("%d %d", &a, &b);
        
        if(pos.count(a) && pos.count(b))
        {
            a = pos[a], b = pos[b];
            int x = a, y = b;
            
            while (a != b)
            {
                if(depth[a] < depth[b]) b = p[b];
                else a = p[a];
            }
            
            if(a != x && a != y) printf("LCA of %d and %d is %d.\n", seq[x], seq[y], seq[a]);
            else if(a == x) printf("%d is an ancestor of %d.\n", seq[a], seq[y]);
            else printf("%d is an ancestor of %d.\n", seq[a], seq[x]);
        }
        else if(pos.count(a) && pos.count(b) == 0)
            printf("ERROR: %d is not found.\n", b);
        else if(pos.count(a) == 0 && pos.count(b))
            printf("ERROR: %d is not found.\n", a);
        else printf("ERROR: %d and %d are not found.\n", a, b);
    }
    return 0;
}

PAT1151:二叉树中的最低公共祖先

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>

using namespace std;
int n, m;
const int N = 10010;
int in[N], pre[N], seq[N];
unordered_map<int, int> pos;
int depth[N], p[N];
int build(int il, int ir, int pl, int pr, int d)
{
    int root = pre[pl];
    int k = root;
    
    depth[root] = d;
    if(il < k) p[build(il, k - 1, pl + 1, pl + 1 + k - 1 - il, d + 1)] = root;
    if(ir > k) p[build(k + 1, ir, pl + 1 + k - 1 - il + 1, ir, d + 1)] = root;
    
    return root;
}
int main()
{
    cin >> m >> n;
    for (int i = 0; i < n; i ++ )
    {
        scanf("%d", &seq[i]);
        pos[seq[i]] = i;
        in[i] = i;
    }
    
    for (int i = 0; i < n; i ++ )
    {
        scanf("%d", &pre[i]);
        pre[i] = pos[pre[i]];
    }
    
    build(0, n - 1, 0, n - 1, 0);
    
    while (m -- )
    {
        int a, b;
        scanf("%d %d", &a, &b);

        if(pos.count(a) && pos.count(b))
        {
            a = pos[a], b = pos[b];
            int x = a, y = b;

            while (a != b)
            {
                if(depth[a] < depth[b]) b = p[b];
                else a = p[a];
            }

            if(a != x && a != y) printf("LCA of %d and %d is %d.\n", seq[x], seq[y], seq[a]);
            else if(a == x) printf("%d is an ancestor of %d.\n", seq[a], seq[y]);
            else printf("%d is an ancestor of %d.\n", seq[a], seq[x]);
        }
        else if(pos.count(a) && pos.count(b) == 0)
            printf("ERROR: %d is not found.\n", b);
        else if(pos.count(a) == 0 && pos.count(b))
            printf("ERROR: %d is not found.\n", a);
        else printf("ERROR: %d and %d are not found.\n", a, b);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shirandexiaowo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值