ZYB's Premutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 31 Accepted Submission(s): 8
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
题意:给定1-n某一个全排列的前缀区间逆序对个数,让你还原这个序列。
思路:线段树插空,逆向跑一遍就ok了。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (50000+10)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
int a[MAXN], b[MAXN];
struct Tree
{
int l, r, len;
int sum;
};
Tree tree[MAXN<<2];
void PushUp(int o){
tree[o].sum = tree[ll].sum + tree[rr].sum;
}
void Build(int o, int l, int r)
{
tree[o].l = l; tree[o].r = r;
tree[o].len = r - l + 1;
tree[o].sum = 1;
if(l == r)
return ;
int mid = (l + r) >> 1;
Build(lson); Build(rson);
PushUp(o);
}
int Query(int o, int v)
{
if(tree[o].l == tree[o].r)
return tree[o].l;
int mid = (tree[o].l + tree[o].r) >> 1;
if(tree[ll].sum >= v)
return Query(ll, v);
else
return Query(rr, v-tree[ll].sum);
}
void Update(int o, int P)
{
if(tree[o].l == tree[o].r)
{
tree[o].sum = 0;
return ;
}
int mid = (tree[o].l + tree[o].r) >> 1;
if(P <= mid)
Update(ll, P);
else
Update(rr, P);
PushUp(o);
}
int main()
{
int t; Ri(t);
W(t)
{
int n; Ri(n);
for(int i = 1; i <= n; i++)
Ri(a[i]);
a[0] = 0; Build(1, 1, n);
for(int i = n; i >= 1; i--)
{
int num = a[i] - a[i-1];
int P = Query(1, num+1);
//Pi(P);
Update(1, P);
b[i] = n-P+1;
}
for(int i = 1; i <= n; i++)
{
if(i > 1) printf(" ");
printf("%d", b[i]);
}
printf("\n");
}
return 0;
}