NC51101 Lost Cows

这篇具有很好参考价值的文章主要介绍了NC51101 Lost Cows。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

题目链接

题目

题目描述

\(N (2 \leq N \leq 8,000)\) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.

Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.

Given this data, tell FJ the exact ordering of the cows.

输入描述

  • Line 1: A single integer, N
  • Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.

输出描述

  • Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.

示例1

输入

5
1
2
1
0

输出

2
4
5
3
1

题解

知识点:树状数组,倍增,枚举。

首先需要一个事实,对于一个排列,要确定其中某个位置的数具体是多少,可以通过整个排列比它小的数字有多少个(或者比它大的数字有多少个)确定。现在这道题确定了各个位置的数的左边比它小的数的个数,我们只需要知道在它右边有多少个数比它小就行,因此我们从右往左枚举,依次确定数字。

首先用树状数组维护右侧出现的数字,随后需要二分一个 \(x\) 通过比较 \(x - cnt_{<x} -1 < a_i\) 得知 \(x\) 是否小了还是大了,从而找到第一个 \(x - cnt_{<x} -1 = a_i\) 的点。注意,条件不能为 \(x - cnt_{<x} -1 \leq a_i\) , 因为可能会出现连续一段刚好等于 \(a_i\) 的点,而我们只需要第一个的下标即可,如果用这个条件,我们得到的是最后一个下标。

当然,这个二分条件其实还可以更简单,我们反过来记录右侧没出现的数字,那么 \(cnt_{<x}\) 直接代表左侧比 \(x\) 小的数字个数,那么条件为 \(cnt_{<x} < a_i\) 即可。

另外,二分套树状数组查询的复杂度是对数平方的,并不是最优的。我们可以直接使用树状数组本身的倍增性质进行二分,是最优的对数复杂度。我封装了两个常用的函数,大于等于 lower_bound 和大于 upper_bound

这道题查询 \(cnt_{<x} = a_i\) 的第一个点,我们 lower_bound 查询即可。

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

空间复杂度 \(O(n)\)文章来源地址https://www.toymoban.com/news/detail-432544.html

代码

#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) { return query(r) - query(l - 1); }

    int lower_bound(T val) {
        int pos = 0;
        for (int i = 1 << __lg(n); i; i >>= 1) {
            if (pos + i <= n && node[pos + i] < val) {
                pos += i;
                val -= node[pos];
            }
        }
        return pos + 1;
    }
    int upper_bound(T val) {
        int pos = 0;
        for (int i = 1 << __lg(n); i; i >>= 1) {
            if (pos + i <= n && node[pos + i] <= val) {
                pos += i;
                val -= node[pos];
            }
        }
        return pos + 1;
    }
};

int a[8007];
int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n;
    cin >> n;
    for (int i = 2;i <= n;i++) cin >> a[i];
    Fenwick<int> fw(n);
    for (int i = 1;i <= n;i++) fw.update(i, 1);

    vector<int> ans(n + 1);
    for (int i = n;i >= 1;i--) {
        ans[i] = fw.lower_bound(a[i] + 1);
        fw.update(ans[i], -1);
    }
    for (int i = 1;i <= n;i++) cout << ans[i] << '\n';
    return 0;
}

到了这里,关于NC51101 Lost Cows的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 长春理工大学第六届CTF网络攻防大赛题解(文末有题目下载链接)

    此题解仅为部分题解,包括: 【RE】:①Reverse_Checkin ②SimplePE ③EzGame 【Web】①f12 ②ezrunner 【Crypto】①MD5 ②password ③看我回旋踢 ④摩丝 【Misc】①爆爆爆爆 ②凯撒大帝的三个秘密 ③你才是职业选手 ① Reverse Checkin: 双击文件看到如上提示:“也许你能从字符串里找到什么”

    2024年02月05日
    浏览(49)
  • 微信分享第三方连接(H5页面)自定义缩略图、标题、描述(显示分享框,而不是链接)(微信JS-SDK)

    首先要说明几个分享相关的问题: 现在微信不支持自定义按钮分享组件(也就是说不能点击某个按钮分享),只能通过微信右上角的三个小点,点击后选择分享给朋友,朋友圈等。 当前从企业微信分享到微信好友和微信朋友圈是有问题的,一些手机(有些是因为app版本,企

    2024年02月09日
    浏览(43)
  • Animoca Brands 开源计时赛游戏模式的相关代码

    Animoca Brands 和 REVV Motorsport 今天宣布计时赛模式已成为一个开源项目。计时赛是由 Animoca Brands 开发的 F1® Delta Time 中的一种游戏模式,由于设置和配置成功的赛车项目所涉及的精确和高度竞争的要求,该模式非常受具有战略头脑的玩家的欢迎。 计时赛于 2020 年 3 月 30 日向大

    2024年02月16日
    浏览(21)
  • Brandes算法计算无向图节点最短路径之和-Java代码实现

            Brandes算法是一种经典的计算中介中心性的算法,它通过计算节点对之间的最短路径数量来评估节点的中介中心性。在复杂网络分析中,中介中心性是一种常用的指标,用于衡量节点在网络中的重要性程度。中介中心性越高的节点往往具有更大的影响力,可能成为

    2024年04月23日
    浏览(18)
  • Uncaught (in promise) TypeError: Cannot read properties of null (reading ‘brands)

    在写vue项目时我们经常会遇见这种报错, 报错: Uncaught (in promise) TypeError: Cannot read properties of null (reading \\\'brands\\\') 这句话意思是:无法读取null属性(读取\\\'brands\\\')  解决办法是在需要渲染的地方加一个v-if来判断数据存在 如下图 搞定!! 

    2024年02月11日
    浏览(37)
  • sqlalchemy 报错 lost connection 解决

    最近在开发过程中遇到一个sqlalchemy lost connection的报错,记录解决方法。 python后端开发,使用的框架是Fastapi + sqlalchemy。在一个接口请求中报错如下: 主要报错信息是: sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, \\\'Lost connection to MySQL server during query\\\') 在网上搜了很多

    2023年04月10日
    浏览(35)
  • sqlalchemy 报错 Lost connection to MySQL server during query 解决

    最近在开发过程中遇到一个sqlalchemy lost connection的报错,记录解决方法。 python后端开发,使用的框架是Fastapi + sqlalchemy。在一个接口请求中报错如下: 主要报错信息是: sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, \\\'Lost connection to MySQL server during query\\\') 在网上搜了很多

    2023年04月10日
    浏览(29)
  • Lost in the Middle: How Language Models Use Long Contexts

    本文是LLM系列文章,针对《Lost in the Middle: How Language Models Use Long Contexts》的翻译。 虽然最近的语言模型能够将长上下文作为输入,但人们对它们使用长上下文的情况知之甚少。我们分析了语言模型在两项任务中的性能,这两项任务需要在输入上下文中识别相关信息:多文档问

    2024年02月09日
    浏览(33)
  • 论文解读: 2023-Lost in the Middle: How Language Models Use Long Contexts

    大模型使用的关键在于Prompt,然而大模型存在幻觉现象,如何减少这种现象的发生成为迫切解决的问题。外部知识库+LLM的方法可以缓解大模型幻觉,但是如何撰写Prompt才能发挥LLM的性能。下面介绍这篇论文说明上下文信息出现在Prompt什么位置使模型表现最佳,以及上下文文本

    2024年02月17日
    浏览(35)
  • MySQL----MySQL数据库出现Lost connection to MySQL server during query错误的解决办法

    【原文链接】MySQL----MySQL数据库出现Lost connection to MySQL server during query错误的解决办法 Mysql数据库在查询数据库的时候回报出了如下异常:Lost connection to MySQL server during query,具体异常信息如下: 1、在数据库中查看如下变量的值 可以看到这里的net_read_timeout和net_write_timeout分别

    2024年02月16日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包