首页 > 其他分享 >NC24048 [USACO 2017 Jan P]Promotion Counting

NC24048 [USACO 2017 Jan P]Promotion Counting

时间:2023-06-23 17:22:51浏览次数:46  
标签:cow int sum USACO Jan leq manager NC24048 ans

题目链接

题目

题目描述

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…N (\(1\leq N\leq 100,000\)), 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).

输入描述

The first line of input contains N.
The next N lines of input contain the proficiency ratings p(1)…p(N) for the cows. Each is a distinct integer in the range 1…1,000,000,000.
The next N−1 lines describe the manager (parent) for cows 2…N. Recall that cow 1 has no manager, being the president.

输出描述

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

示例1

输入

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

输出

2
0
1
0
0

题解

知识点:DFS序,树状数组。

普通逆序对的公式是:

\[\sum_{i=1}^n \sum_{j<i} [a_j > a_i] \]

即从左到右枚举用时间轴维护先后关系,用树状数组维护数字大小关系。

类似地,我们求出树的dfn序,得到以 \(u\) 为根节点的子树的开始和结束序号 \(L_u,R_u\) ,则树上逆序对公式是:

\[\sum_{i=1}^n \sum_{L_i\leq j \leq R_i} [a_j > a_i] \]

其中 \(L_i \leq j \leq R_i\) 需要时间轴的版本,单纯枚举是处理不了的。我们可以考虑用时间轴维护大小关系,先将 \(a\) 从大到小排序,那么公式转换为:

\[\sum_{i = 1}^n \sum_{a_j > a_i} [L_i \leq j \leq R_i] \]

其中 \(\displaystyle \sum_{a_j > a_i} [L_i \leq j \leq R_i]\) ,在时间轴维护大小关系下,用树状数组即可求出区间出现过的数字个数。

时间复杂度 \(O(n \log n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template<class T>
class Fenwick {
    int n;
    vector<T> node;

public:
    Fenwick(int _n = 0) { init(_n); }

    void init(int _n) {
        n = _n;
        node.assign(n + 1, T());
    }

    void update(int x, T val) { for (int i = x;i <= n;i += i & -i) node[i] += val; }

    T query(int x) {
        T ans = T();
        for (int i = x;i >= 1;i -= i & -i) ans += node[i];
        return ans;
    }
    T query(int l, int r) {
        T ans = T();
        ans += query(r);
        ans -= query(l - 1);
        return ans;
    }
};

struct T {
    int sum;
    T &operator+=(const T &x) { return sum += x.sum, *this; }
    T &operator-=(const T &x) { return sum -= x.sum, *this; }
};

const int N = 100007;
pair<int, int> a[N];
vector<int> g[N];

int dfncnt;
int L[N], R[N];
void dfs(int u) {
    L[u] = ++dfncnt;
    for (auto v : g[u]) dfs(v);
    R[u] = dfncnt;
}

int ans[N];

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> a[i].first, a[i].second = i;
    sort(a + 1, a + n + 1, greater<pair<int, int>>());
    for (int i = 2;i <= n;i++) {
        int u;
        cin >> u;
        g[u].push_back(i);
    }
    dfs(1);

    Fenwick<T> fw(n);
    for (int i = 1;i <= n;i++) {
        int u = a[i].second;
        ans[u] = fw.query(L[u], R[u]).sum;
        fw.update(L[u], { 1 });
    }

    for (int i = 1;i <= n;i++) cout << ans[i] << '\n';
    return 0;
}

标签:cow,int,sum,USACO,Jan,leq,manager,NC24048,ans
From: https://www.cnblogs.com/BlankYang/p/17499388.html

相关文章

  • 花朵识别系统Python+TensorFlow+Django+卷积神经网络算法实现
    一、背景花朵识别系统,基于Python实现,深度学习卷积神经网络,通过TensorFlow搭建卷积神经网络算法模型,并对数据集进行训练最后得到训练好的模型文件,并基于Django搭建可视化操作平台。在当今信息化社会,图像识别技术在各种领域都展现出了重要的应用价值,包括医学影像分析、自动驾驶、......
  • django中使用redis
    django中使用redis方法1,通用安装redis#pipinstallredis#1写一个连接池 importredis.ConnectionPool(host='xx.xx.xx.xx',port=6379,password='xxx',max_connections=1000)#2在使用地方导入即可 conn=redis.Redis(connection_pool=pool)conn.incr(�......
  • [USACO18DEC]Balance Beam P
    [USACO18DEC]BalanceBeamP热爱卡精度的你,为什么分数不取模?既然不去模,那么拿到这个题先想想能不能乱搞过去。设\(f_{i,j}\)表示\(i\)点出发至多走\(j\)次的最优期望报酬。当\(j\rightarrow+\infty\)时视为答案。转移为\[f_{i,j}=\max\{f_{i,j-1},\frac{f_{i......
  • 蔬菜识别系统Python+TensorFlow+Django+卷积神经网络算法
    一、介绍蔬菜识别系统,使用Python作为主要开发语言,基于深度学习TensorFlow框架,搭建卷积神经网络算法。并通过对数据集进行训练,最后得到一个识别精度较高的模型。并基于Django框架,开发网页端操作平台,实现用户上传一张图片识别其名称。二、效果图片三、演示视频+代码视频+完整......
  • 鸟类识别系统Python+Django+TensorFlow+卷积神经网络算法【完整代码】
    一、介绍鸟类识别系统,使用Python作为主要开发语言,基于深度学习TensorFlow框架,搭建卷积神经网络算法。并通过对数据集进行训练,最后得到一个识别精度较高的模型。并基于Django框架,开发网页端操作平台,实现用户上传一张图片识别其名称。数据集选自加州理工学院200种鸟类数据集二、......
  • 蔬菜识别系统Python+TensorFlow+Django+卷积神经网络算法
    一、介绍蔬菜识别系统,使用Python作为主要开发语言,基于深度学习TensorFlow框架,搭建卷积神经网络算法。并通过对数据集进行训练,最后得到一个识别精度较高的模型。并基于Django框架,开发网页端操作平台,实现用户上传一张图片识别其名称。二、效果图片三、演示视频+代码视频+完整代码:http......
  • 自动化平台总结(httprunner+djangorestframework+python3+Mysql+Vue)【基础构思】
    一、前言最近从零搭建了一个自动化测试平台,虽然不是第一次从零搭建,但是也从来没有进行过这类搭建的总结,还是记录一下,搭建过程中的一些问题和方法。方便以后总结和翻阅二、简介搭建的平台使用的是Python3.6,未来有空可能考虑加个java版本。前端用的Vue,主体是httprunner2.......
  • Django与celery集成:异步任务原理和过程
    0.原理和架构a.客户发送请求到django;b.django产生任务(要执行的函数);c.django把任务丢给celery的brokerd.celery的worker从broker拿到任务并且执行;e.worker执行后保存结果到后端数据库;  1.在django里面配置celery的目录结构PSD:\djangotest\myrecrument>treeD:.├─.idea......
  • Django 日志配置
    Django项目日志配置记录业务运行过程中的一些关键信息,方便查看程序运行情况以及排查报错等详细日志配置settings.py配置文件中新增日志配置#设置时区,日志输出时间为utc-8时区#TIME_ZONE='UTC'TIME_ZONE='Asia/Shanghai'#日志配置LOGGING={'versio......
  • 通用密钥,无需密码,在无密码元年实现Passkeys通用密钥登录(基于Django4.2/Python3.10)
    毋庸讳言,密码是极其伟大的发明,但拜病毒和黑客所赐,一旦密码泄露,我们就得绞尽脑汁再想另外一个密码,但记忆力并不是一个靠谱的东西,一旦遗忘密码,也会造成严重的后果,2023年业界巨头Google已经率先支持了Passkeys登录方式,只须在设备上利用PIN码解锁、指纹或面部辨识等生物识别方式,即可验......