洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数


题目描述

The cows have once again tried to form a startup company, failing to remember from past experience that cows make terrible managers!

The cows, conveniently numbered $1 \ldots N\left( 1\leq N\leq 100,000\right)$ organize the company as a tree, with cow 1 as the president (the root of the tree). Each cow except the president has a single manager (its "parent" in the tree). Each cow $i$ has a distinct proficiency rating, $p(i)$, which describes how good she is at her job. If cow $i$ is an ancestor (e.g., a manager of a manager of a manager) of cow $j$, then we say $j$ is a subordinate of $i$.

Unfortunately, the cows find that it is often the case that a manager has less proficiency than several of her subordinates, in which case the manager should consider promoting some of her subordinates. Your task is to help the cows figure out when this is happening. For each cow $i$ in the company, please count the number of subordinates $j$ where $p(j)>p(i)$.

奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者!

为了方便,把奶牛从 $1 \ldots N\left( 1\leq N\leq 100,000\right)$ 编号,把公司组织成一棵树,1 号奶牛作为总裁(这棵树的根节点)。除了总裁以外的每头奶牛都有一个单独的上司(它在树上的 “双亲结点”)。所有的第 $i$ 头牛都有一个不同的能力指数 $p(i)$,描述了她对其工作的擅长程度。如果奶牛 $i$ 是奶牛 $j$ 的祖先节点(例如,上司的上司的上司),那么我们我们把奶牛 $j$ 叫做 $i$ 的下属。

不幸地是,奶牛们发现经常发生一个上司比她的一些下属能力低的情况,在这种情况下,上司应当考虑晋升她的一些下属。你的任务是帮助奶牛弄清楚这是什么时候发生的。简而言之,对于公司的中的每一头奶牛 $i$,请计算其下属 $j$ 的数量满足 $p(j)>p(i)$。
输入输出格式
输入格式:

The first line of input contains $N$.

The next NNN lines of input contain the proficiency ratings $p(1) \ldots p(N)$ for the cows. Each is a distinct integer in the range $1 \ldots 1,000,000,000$.

The next $N-1$ lines describe the manager (parent) for cows $2 \ldots N$. Recall that cow 1 has no manager, being the president.

输入的第一行包括一个整数 $N$。

接下来的 $N$ 行包括奶牛们的能力指数 $p(1) \ldots p(N)$. 保证所有数互不相同,在区间 $1 \ldots 10^9$之间。

接下来的 $N−1$ 行描述了奶牛 $2 \ldots N$ 的上司(双亲节点)的编号。再次提醒,1 号奶牛作为总裁,没有上司。

输出格式:

Please print $N$ lines of output. The $i$th line of output should tell the number of subordinates of cow $i$ with higher proficiency than cow $i$.

输出包括 $N$ 行。输出的第 $i$ 行应当给出有多少奶牛 $i$ 的下属比奶牛 $i$ 能力高。

输入输出样例
输入样例#1: 复制

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

输出样例#1: 复制

2
0
1
0
0

说明

感谢@rushcheyo 的翻译

这题有很多做法。但我目前只会一种。慢慢update。

 

解法一:dfs序+树状数组。

先求出dfs序和每个节点子树中节点个数,然后把所有节点按权值从大到小排。从大到小插入树状数组,插入之前查询一下dfs序区间和就OK了。

#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }
    return x * f;
}

const int N = 1e5 + 10;
vector<int> G[N];
struct P {
    int u, val;
    bool operator < (const P &rhs) const {
        return val > rhs.val;
    }
} p[N];
int dfn[N], size[N], n, cnt, tree[N];

inline int lowbit(int x) { return x & -x; }

void add(int x) {
    for (int i = x; i <= n; tree[i]++, i += lowbit(i));
}

int query(int x) {
    int res = 0;
    for (int i = x; i; res += tree[i], i -= lowbit(i));
    return res; 
}

void dfs(int u, int fa) {
    size[u] = 1;
    dfn[u] = ++cnt;
    for (auto v : G[u]) {
        if (v == fa) continue;
        dfs(v, u);
        size[u] += size[v];
    }
}

int ans[N];

int main() {
    n = read();
    for (int i = 1; i <= n; i++) {
        p[i].val = read();
        p[i].u = i;
    }
    for (int i = 2; i <= n; i++) {
        int u = read();
        G[u].eb(i);
    }
    dfs(1, 1);
    sort(p + 1, p + n + 1);
    for (int i = 1; i <= n; i++) {
        int u = p[i].u;
        ans[u] = query(dfn[u] + size[u] - 1) - query(dfn[u]);
        add(dfn[u]); 
    }
    for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
    return 0;
}
View Code

 

解法二:线段树合并

先离散化,对每个点建一个权值线段树,然后从下往上合并,答案即为当前权值线段树[a[u] + 1, n]的区间和(这玩意好像还挺有趣的?

#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }
    return x * f;
}

const int N = 1e5 + 10;
int rt[N], cnt, n, sum[20 * N], rs[N * 20], ls[N * 20], a[N], ans[N];
vector<int> G[N];
struct P { 
    int v, id;
    bool operator < (const P &rhs) const {
        return v < rhs.v;
    }
} p[N];

inline void pushup(int u) {
    sum[u] = sum[ls[u]] + sum[rs[u]];
}

void build(int &u, int l, int r, int x) {
    if (!u) u = ++cnt;
    if (l == r) {
        sum[u]++;
        return;
    }
    int mid = l + r >> 1;
    if (x <= mid) build(ls[u], l, mid, x);
    else build(rs[u], mid + 1, r, x);
    pushup(u);
}

int merge(int u, int v) {
    if (!u || !v) return u + v;
    ls[u] = merge(ls[u], ls[v]);
    rs[u] = merge(rs[u], rs[v]);
    pushup(u);
    return u;
}

int query(int u, int l, int r, int x, int y) {
    if (!u) return 0;
    if (x <= l && y >= r) return sum[u];
    int mid = l + r >> 1;
    int ans = 0;
    if (x <= mid) ans += query(ls[u], l, mid, x, y);
    if (y > mid) ans += query(rs[u], mid + 1, r, x, y);
    return ans;
}

void dfs(int u) {
    for (auto v : G[u]) {
        dfs(v);
        rt[u] = merge(rt[u], rt[v]);
    }
    ans[u] = query(rt[u], 1, n, a[u] + 1, n);
}

int main() {
    n = read();
    for (int i = 1; i <= n; i++) {
        p[i].v = read();
        p[i].id = i;
    }
    sort(p + 1, p + n + 1);
    for (int i = 1; i <= n; i++) {
        a[p[i].id] = i;
        build(rt[p[i].id], 1, n, i);
    }
    for (int i = 2; i <= n; i++) {
        int u = read();
        G[u].eb(i);
    }
    dfs(1);
    for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
}
View Code

 

转载于:https://www.cnblogs.com/Mrzdtz220/p/11135900.html

内容概要:该论文研究了一种基于粒子群优化(PSO)的STAR-RIS辅助NOMA无线通信网络优化方法。STAR-RIS作为一种新型可重构智能表面,能够同时反射和传输信号,与传统的仅能反射的RIS不同。结合NOMA技术,可以提高覆盖范围、同时服务的用户数量和频谱效率。由于STAR-RIS元素众多,获取完整信道状态信息(CSI)开销大,因此作者提出在不依赖完整CSI的情况下,联合优化功率分配、基站波束成形以及STAR-RIS的传输和反射波束成形向量,以最大化总可实现速率,同时保证每个用户的最低速率要求。仿真结果表明,该方案优于STAR-RIS辅助的OMA系统。论文还提供了详细的Python代码实现,包括系统参数设置、信道模型、速率计算、目标函数、约束函数、主优化函数和结果可视化等内容,完整再现了论文中的关键技术方案。 适合人群:通信工程领域的研究人员、高校教师和研究生,特别是对智能反射面技术、非正交多址接入技术和智能优化算法感兴趣的读者。 使用场景及目标:①研究和开发基于STAR-RIS的无线通信系统;②探索PSO算法在无线通信优化中的应用;③评估STAR-RIS-NOMA系统相对于传统OMA系统的性能优势;④为实际通信系统设计提供理论依据和技术支持。 其他说明:该论文不仅提出了创新的技术方案,还提供了完整的代码实现,便于读者理解和复现实验结果。此外,论文还讨论了与其他优化方法(如DDPG)的对比,并分析了不同工作协议(如模式切换、时间切换和能量分配)的性能差异,进一步丰富了研究内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值