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)\)
代码
#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的更多相关文章
- [LeetCode] Bulls and Cows 公母牛游戏
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- [Leetcode] Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列
第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...
- POJ2186 Popular Cows [强连通分量|缩点]
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31241 Accepted: 12691 De ...
- Poj2186Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31533 Accepted: 12817 De ...
- [poj2182] Lost Cows (线段树)
线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...
- 【POJ3621】Sightseeing Cows
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8331 Accepted: 2791 ...
随机推荐
- 使用hugo在gitee上写blog
1. 安装hugo 1)下载 Hugo Releases,选择hugo_xxx_Windows-64bit.zip(xxx位版本). 2)设置路径 我的电脑->属性->高级系统设置-> ...
- .net Core5.0使用IdentityServer4 进行权限控制
.net Core5.0使用IdentityServer4 进行权限控制 IdentityServer4 ASP.NET Core的一个包含OpenID Connect和OAuth 2.0协议的框架 ...
- DeepMind公司最新ai技术参加Codeforces击败大部分选手
著名的编程竞赛网站Codeforces发布了一篇名为<AlphaCode(DeepMind) Solves Programming Problems on CodeForce>的文章,将阿 ...
- VSCode 快捷键,简化操作
一. 区域代码快捷键 1. 折叠所有 折叠所有区域代码的快捷: ctrl + k ctrl + 0 ; 展开所有折叠区域代码的快捷:ctrl +k ctrl + J ; 2. 按层 ...
- 使用 Vue 3 时应避免的 10 个错误
Vue 3已经稳定了相当长一段时间了.许多代码库都在生产环境中使用它,其他人最终都将不得不迁移到Vue 3.我现在有机会使用它并记录了我的错误,下面这些错误你可能想要避免. 使用Reactive声明原 ...
- Android笔记--Android+数据库的增加数据的实现
具体实现 添加成功: 界面代码很简单,直接忽略. 连接数据库的代码: Connect.java package com.example.myapplication.database; import a ...
- 《HelloTester》第3期
1.前言 读了之前的简历篇和投递篇,你应该开始准备迎接面试了,那么,在面试前,我们都需要准备些什么呢?是要默默的去背诵那些理论知识,还是熟悉一下自己的技能?又或者去看看公司要求的那些技术呢?接下来这篇 ...
- [思维提升|干货All in]6种算法解决LeetCode困难题:滑动窗口最大值
为了更好的阅读体验,欢迎阅读原文: [思维提升|干货All in]6种算法解决LeetCode困难题:滑动窗口最大值 (eriktse.com) 最近在leetcode遇到一道非常经典的题目:239. ...
- GKCTF X DASCTF应急挑战杯-Maple_root-Writeup
GKCTF X DASCTF应急挑战杯-Maple_root-Writeup 参赛队员: b4tteRy, x0r, f1oat 最终成绩:2285 最终排名:27 总结 经过最近几次类线下的演练,感 ...
- 使用 screw(螺丝钉) 快速生成数据库文档
一.框架介绍 回想起那个去年的7月份,第一份实习,组长让我写一份金蝶云的SQL文档,当时一看2000多张表,当时就猛吸一根烟,然后去gitee看看有没有好的框架快速生成 SQL 文档 ,由此找到了 s ...