ZYB's Premutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1141 Accepted Submission(s): 601
Problem Description
ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ask you to
restore the premutation.
Pair (i,j)(i<j) is considered as a reverse log if Ai>Aj is matched.
Input
In the first line there is the number of testcases T.
For each teatcase:
In the first line there is one number N.
In the next line there are N numbers Ai,describe the number of the reverse logs of each prefix,
The input is correct.
1≤T≤5,1≤N≤50000
Output
For each testcase,print the ans.
Sample Input
1 3 0 1 2
Sample Output
3 1 2
Source
Recommend
hujie | We have carefully selected several similar problems for you: 6331 6330 6329 6328 6327
Statistic | Submit | Discuss | Note
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
#define maxn 100005
#define MAX 1000000000
#define ms memset
using namespace std;
int n , seq[maxn];
int tree[maxn];
int lowbit(int x) { return x & (-x) ; }
int sum(int x)
{
int res=0;
for(;x>0;res+=tree[x],x-=lowbit(x));
return res;
}
void add(int x,int d) { for( ; x<=n ; tree[x]+=d , x+=lowbit(x) ) ; }
/*
题目大意:给定n个数,
和序列前缀的逆序对,要求还原这个序列。
这题要根据逆序对序列来倒推,
s[i]-s[i-1]就是这个位置放的数的特征。
放完后用树状数组维护,找位置用二分在树状数组中查找。
当然,自己手工模拟一下就知道找的应该是下界。
*/
int main()
{
int t;scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&seq[i]);
for(int i=1;i<=n;i++) add(i,1);
int ans[maxn];
for(int i=n;i>=1;i--)
{
int pos=seq[i]-seq[i-1];
pos=i-pos;
int l=1,r=n,op=-1;
while(r>=l)
{
int mid=(l+r)>>1;
if(sum(mid)>=pos)
{
op=mid;
r=mid-1;
}
else l=mid+1;
}
ans[i]=op;
add(op,-1);
}
for(int i=1;i<=n;i++) printf("%d%c",ans[i],i==n?'\n':' ');
}
return 0;
}