[Codeforces 1208D]Restore Permutation (树状数组)
[Codeforces 1208D]Restore Permutation (树状数组)
题面
有一个长度为n的排列a。对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i}^i a_j\).即前面比它小的元素的值之和。
给出\(s_1,s_2 \dots s_n\),求a
分析
考虑如何求\(a_n\),\(s_n\)实际上表示的是1~n中比\(a_n\)小的所有数的和,可以直接求出\(a_n\)
然后我们可以倒序求\(a_i\),求到\(a_i\)的时候,我们已经知道\(a_i\)前面有哪些数,只是不知道顺序。维护一个数据结构存储这些数的集合,并存储从小到大排列后的前缀和,然后二分出最后一个前缀和\(\leq s_i\)的位置。
比如最后一个样例:
input:
5
0 1 1 1 10
output:
1 4 3 2 5
我们求到第4个数的时候,由于第5个数已经求出来是5,那前面的数就是{1,2,3,4},2之前的前缀和是1,1之前的前缀和是0,那么二分出的点就是2.
具体实现可以用树状数组,初始时候给第i个位置加i,求出某个数的值为x之后把第x个位置-x,相当于把x移出集合
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#define maxn 200000
using namespace std;
typedef long long ll;
int n;
struct fenwick_tree{
ll c[maxn+5];
inline int lowbit(int x){
return x&(-x);
}
inline void update(int x,int v){
for(int i=x;i<=n;i+=lowbit(i)){
c[i]+=v;
}
}
inline ll query(int x){
ll ans=0;
for(int i=x;i>0;i-=lowbit(i)) ans+=c[i];
return ans;
}
}T;
ll a[maxn+5];
int ans[maxn+5];
int bin_search(int l,int r,ll sum){
int ans=n;
int mid;
while(l<=r){
mid=(l+r)>>1;
if(T.query(mid-1)<=sum){
ans=mid;
l=mid+1;
}else r=mid-1;
}
return ans;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%I64d",&a[i]);
}
for(int i=1;i<=n;i++) T.update(i,i);
for(int i=n;i>=1;i--){
int k=bin_search(1,n,a[i]);
ans[i]=k;
T.update(k,-k);
}
for(int i=1;i<=n;i++) printf("%d ",ans[i]);
}
[Codeforces 1208D]Restore Permutation (树状数组)的更多相关文章
- D. Restore Permutation 树状数组+二分
D. Restore Permutation 题意:给定n个数a[i],a[ i ]表示在[b[1],b[i-1]]这些数中比 b[i]小的数的和,要你构造这样的b[i]序列 题解:利用树状数组 求比 ...
- Codeforces 650D - Zip-line(树状数组)
Codeforces 题目传送门 & 洛谷题目传送门 我怕不是个 nt--一开始忽略了"询问独立"这个条件--然后就一直在想有什么办法维护全局 LIS--心态爆炸 首先离散 ...
- UVA 11525 Permutation(树状数组)
题目意思是说 给你一个数k 然后有k个si 问你1--k 的第n个全排列是多少 注意是 1 2 3...k的全排列 不是si的 N= 由观察得知(k-i)!就是k-i个数字的全排列种数 ...
- Codeforces 1139F Dish Shopping 树状数组套平衡树 || 平衡树
Dish Shopping 将每个物品拆成p 和 s 再加上人排序. 然后问题就变成了, 对于一个线段(L - R), 问有多少个(li, ri)满足 L >= li && R ...
- Codeforces 830B - Cards Sorting 树状数组
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CodeForces 522D Closest Equals 树状数组
题意: 给出一个序列\(A\),有若干询问. 每次询问某个区间中值相等且距离最短的两个数,输出该距离,没有则输出-1. 分析: 令\(pre_i = max\{j| A_j = A_i, j < ...
- codeforces 589G G. Hiring(树状数组+二分)
题目链接: G. Hiring time limit per test 4 seconds memory limit per test 512 megabytes input standard inp ...
- CodeForces–830B--模拟,树状数组||线段树
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces 960F Pathwalks ( LIS && 树状数组 )
题意 : 给出若干个边,每条边按照给出的顺序编号,问你找到一条最长的边权以及边的编号同时严格升序的一条路径,要使得这条路径包含的边尽可能多,最后输出边的条数 分析 : 这题和 LIS 很相似,不同的 ...
随机推荐
- DOM-document 对象
Document 对象Document 对象代表整个HTML 文档,可用来访问页面中的所有元素.Document 对象是 Window 对象的一个部分,可通过 window.document 属性来访 ...
- docker安装MySQL5.7示例!!坑
docker pull mysql 一.错误的启动 [root@localhost ~]# docker run ‐‐name mysql01 ‐d mysql 42f0981990 ...
- bzoj2600 [Ioi2011]ricehub 双指针
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2600 题解 随便写一个比较简单的 two pointers 练习题. 首先答案肯定是一个原序列 ...
- bzoj2802 [Poi2012]Warehouse Store 贪心+堆
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2802 题解 我一开始想到了一个比较麻烦的做法. 把每一天按照 \(b_i\) 从小到大排序,\ ...
- Django【第2篇】:Django之反向解析
Django框架之第二篇 一.知识点回顾 1.MTV模型 model:模型,和数据库相关的 template:模板,存放html文件,模板语法(目的是将变量如何巧妙的嵌入到HTML页面中). view ...
- 7.docker私有registry
一.Docker Registry分类 Registry用于保存docker镜像,包括镜像的层次结构和元数据.都是基于https或者http工作的. 用户可自建Registry,也可使用官方的Dock ...
- H5 FormData对象
FormData对象 2018年01月08日 14:31:53 阅读数:2635 FormData对象,可以把所有表单元素的name与value组成一个queryString,提交到后台. 在使用aj ...
- man lspci
lspci(8) Linux PCI Utilities lspci(8) NAME lspci - 列出 ...
- Zookeeper实现哨兵机制
master选举使用场景及结构 现在很多时候我们的服务需要7*24小时工作,假如一台机器挂了,我们希望能有其它机器顶替它继续工作.此类问题现在多采用master-salve模式,也就是常说的主从模式, ...
- 【leetcode】1144. Decrease Elements To Make Array Zigzag
题目如下: Given an array nums of integers, a move consists of choosing any element and decreasing it by ...