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.
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
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
BestCoder Round #65
这道题是告诉你前i个数逆序对数的和,
开始想到填1开始,然后在增,发现超时了,
其实从后面往前面找更好找,最后一个数与前面形成
的逆序对数可以知道,那么就可以知道此数是第几小的
数,通过线段树或树状数组可以求出第几小的数。
附上树状数组+二分的代码,不过注意树状数组采用 二分的时候不能直接
返回值,因为树状不是完全有序的,左右个数堆成,所以保存后往左边找就行了。
这道题是告诉你前i个数逆序对数的和,
开始想到填1开始,然后在增,发现超时了,
其实从后面往前面找更好找,最后一个数与前面形成
的逆序对数可以知道,那么就可以知道此数是第几小的
数,通过线段树或树状数组可以求出第几小的数。
#include <stdio.h>
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1
const int maxn = 50005;
int cnt[maxn << 3], a[maxn], ans[maxn], v;
void Build ( int l, int r, int rt )
{
cnt[rt] = r-l+1;
if ( l == r )
return ;
int m = ( l+r ) >> 1;
Build ( lson );
Build ( rson );
}
void PushUP ( int rt )
{
cnt[rt] = cnt[rt << 1]+cnt[rt << 1 | 1];
}
void query ( int q, int l, int r, int rt )
{
if ( l == r )
{
v = l; //用一个变量保存,然后那样就可以直接更新
cnt[rt] = 0;
return ;
}
int m = ( l+r ) >> 1;
if ( q <= cnt[rt << 1] )
query ( q, lson );
else
query ( q-cnt[rt << 1], rson );
PushUP ( rt );
}
int main ( )
{
int T, n;
scanf ( "%d", &T );
while ( T -- )
{
scanf ( "%d", &n );
Build ( 1, n, 1 );
for ( int i = 0; i < n; i ++ )
scanf ( "%d", &a[i] );
for ( int i = n-1; i >= 1; i -- )
{
//将第几小的数转化为第几大的数
query ( cnt[1]-( a[i]-a[i-1] ), 1, n, 1 );
ans[i] = v;
}
query ( 1, 1, n, 1 );
ans[0] = v;
printf ( "%d", ans[0] );
for ( int i = 1; i < n; i ++ )
printf ( " %d", ans[i] );
printf ( "\n" );
}
return 0;
}
附上树状数组+二分的代码,不过注意树状数组采用 二分的时候不能直接
返回值,因为树状不是完全有序的,左右个数堆成,所以保存后往左边找就行了。
#include <stdio.h>
#include <string.h>
inline int lowbit ( int x )
{
return x & -x;
}
const int maxn = 50005;
int a[maxn], cnt[maxn], ans[maxn], n;
void add ( int x, int v )
{
while ( x <= n )
{
cnt[x] = cnt[x]+v;
x = x+lowbit ( x );
}
}
int sum ( int x )
{
int ret = 0;
while ( x > 0 )
{
ret = ret+cnt[x];
x = x-lowbit ( x );
}
return ret;
}
int binary_search ( int l, int r, int k )
//虽然树状数组不是有序的,但是右边肯定比左边大
{
int m, ret = r;
while ( l <= r )
{
m = l+( r-l )/2;
if ( sum ( m ) >= k )
ret = m, r = m-1;
//*这里不能直接返回,因为可能存在个数一样的
else
l = m+1;
}
add ( ret, -1 ); //减掉1
return ret;
}
void print ( int n )
{
for ( int i = 1; i <= n; i ++ )
printf ( "%d ", cnt[i] );
printf ( "\n" );
}
int main ( )
{
int T;
scanf ( "%d", &T );
while ( T -- )
{
scanf ( "%d", &n );
memset ( cnt, 0, sizeof ( cnt ) );
for ( int i = 1; i <= n; i ++ )
{
scanf ( "%d", &a[i] );
add ( i, 1 ); //更新
}
for ( int i = n; i >= 1; i -- )
{
int l = i-( a[i]-a[i-1] ); //第几大的
ans[i] = binary_search ( 1, n, l );
}
printf ( "%d", ans[1] );
for ( int i = 2; i <= n; i ++ )
printf ( " %d", ans[i] );
printf ( "\n" );
}
return 0;
}