D. Restore Permutation

D. Restore Permutation

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

An array of integers p1,p2,…,pn is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2],[1],[1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2],[1,1],[2,3,4].

There is a hidden permutation of length n.

For each index i, you are given si, which equals to the sum of all pj such that j<i and pj<pi. In other words, si is the sum of elements before the i-th element that are smaller than the i-th element.

Your task is to restore the permutation.

Input
The first line contains a single integer n (1≤n≤2⋅105) — the size of the permutation.

The second line contains n integers s1,s2,…,sn (0≤si≤n(n−1)2).

It is guaranteed that the array s corresponds to a valid permutation of length n.

Output
Print n integers p1,p2,…,pn — the elements of the restored permutation. We can show that the answer is always unique.

Examples
inputCopy
3
0 0 0
outputCopy
3 2 1
inputCopy
2
0 1
outputCopy
1 2
inputCopy
5
0 1 1 1 10
outputCopy
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence si are always 0.

In the second example for i=2 it happens that j=1 satisfies the conditions, so s2=p1.

In the third example for i=2,3,4 only j=1 satisfies the conditions, so s2=s3=s4=1. For i=5 all j=1,2,3,4 are possible, so s5=p1+p2+p3+p4=10.


对于这种序列,我们可以考虑从 1 -> n 去构造答案,我们可以试想对于1来说,肯定是最后一个0的位置对不对,然后我们可以让0后面的所有数字就减去1,这样继续找0最后的位置即可。然后对于找到的位置后面所有元素减少 i ,同时我们要注意让当前找到的位置删掉,就是直接赋值为INF即可(当时sb了,赋值为99999999.。。。。1e18就AC了)。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
int n,cnt,res[N];
struct node{
	int l,r,mi,add;
}t[N<<2];
inline void push_up(int p){
	t[p].mi=min(t[p<<1].mi,t[p<<1|1].mi);
}
inline void push_down(int p){
	if(t[p].add){
		t[p<<1].mi-=t[p].add;	t[p<<1|1].mi-=t[p].add;
		t[p<<1].add+=t[p].add;	t[p<<1|1].add+=t[p].add;
		t[p].add=0;
	}
}
void build(int p,int l,int r){
	t[p].l=l; t[p].r=r;
	if(l==r){
		scanf("%lld",&t[p].mi);	return ;
	}
	int mid=l+r>>1;
	build(p<<1,l,mid);	build(p<<1|1,mid+1,r);
	push_up(p);
}
void change(int p,int l,int r,int v){
	if(t[p].l==l&&t[p].r==r){
		t[p].mi-=v;	t[p].add+=v;	return ;
	}
	push_down(p);
	int mid=t[p].l+t[p].r>>1;
	if(r<=mid)	change(p<<1,l,r,v);
	else if(l>mid)	change(p<<1|1,l,r,v);
	else	change(p<<1,l,mid,v),change(p<<1|1,mid+1,r,v);
	push_up(p);
}
void updata(int p,int x){
	if(t[p].l==t[p].r){
		t[p].mi=1e18;	return ;
	}
	int mid=t[p].l+t[p].r>>1;
	if(x<=mid)	updata(p<<1,x);
	else	updata(p<<1|1,x);
	push_up(p);
}
int ask(int p,int x){
	if(t[p].l==t[p].r)	return t[p].l;
	int mid=t[p].l+t[p].r>>1;	push_down(p);
	if(t[p<<1|1].mi<=x)	return ask(p<<1|1,x);
	else	return ask(p<<1,x);
}
signed main(){
	cin>>n;	build(1,1,n);
	for(int i=1;i<=n;i++){
		int x=ask(1,0);	res[x]=i;	updata(1,x);
		if(x!=n)	change(1,x+1,n,i);
	}
	for(int i=1;i<=n;i++)	cout<<res[i]<<' ';puts("");
	return 0;
}
全部评论

相关推荐

明天不下雨了_人机版:让我们大声的说出来:以前的未来就是现在
点赞 评论 收藏
分享
是每个人事都这样与找工作的人这样沟通吗?正常询问不可以吗
超时空记忆丶:这种人适合跟我聊 我能骂得她心里难受一天,这种byd一看就是欠骂,这么好的机会楼主别错过。
点赞 评论 收藏
分享
06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务