题意:给你每个位置对应的逆序数, 求其原序列。
思路: splay从大到小模拟;
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
#define Key_value ch[ch[root][1]][0]
const int MAXN = 500010;
const int INF = 0x3f3f3f3f;
int pre[MAXN],ch[MAXN][2],key[MAXN],size[MAXN];
int root,tot1;
int sum[MAXN],rev[MAXN],same[MAXN];
int lx[MAXN],rx[MAXN],mx[MAXN];
int s[MAXN],tot2;//内存池和容量
int n,q;
void NewNode(int &r,int father,int k)
{
if(tot2) r = s[tot2--];//取的时候是tot2--,存的时候就是++tot2
else r = ++tot1;
pre[r] = father;
ch[r][0] = ch[r][1] = 0;
key[r] = k;
size[r] = 1;
}
void push_up(int r)
{
int lson = ch[r][0], rson = ch[r][1];
size[r] = size[lson] + size[rson] + 1;
}
void push_down(int r)
{
}
void Init()
{
root = tot1 = tot2 = 0;
ch[root][0] = ch[root][1] = size[root] = pre[root] = 0;
NewNode(root,0,-1);
NewNode(ch[root][1],root,-1);
push_up(ch[root][1]);
push_up(root);
}
//旋转,0为左旋,1为右旋
void Rotate(int x,int kind)
{
int y = pre[x];
push_down(y);
push_down(x);
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind]] = y;
if(pre[y])
ch[pre[y]][ch[pre[y]][1]==y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
push_up(y);
}
//Splay调整,将r结点调整到goal下面
void Splay(int r,int goal)
{
push_down(r);
while(pre[r] != goal)
{
if(pre[pre[r]] == goal)
{
push_down(pre[r]);
push_down(r);
Rotate(r,ch[pre[r]][0] == r);
}
else
{
push_down(pre[pre[r]]);
push_down(pre[r]);
push_down(r);
int y = pre[r];
int kind = ch[pre[y]][0]==y;
if(ch[y][kind] == r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
push_up(r);
if(goal == 0) root = r;
}
int Get_kth(int r,int k)
{
push_down(r);
int t = size[ch[r][0]] + 1;
if(t == k)return r;
if(t > k)return Get_kth(ch[r][0],k);
else return Get_kth(ch[r][1],k-t);
}
//在第pos个数后面插入tot个数
void Insert(int pos, int val)
{
Splay(Get_kth(root,pos+1),0);
Splay(Get_kth(root,pos+2),root);
NewNode(Key_value, ch[root][1], val);
push_up(ch[root][1]);
push_up(root);
}
vector<int> ans;
void InOrder(int r)
{
if(!r)return;
push_down(r);
InOrder(ch[r][0]);
if(key[r] != -1)
ans.push_back(key[r]);
InOrder(ch[r][1]);
}
int a[10010];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d",&n)!=EOF)
{
ans.clear();
Init();
for(int i=1; i<=n; i++)
scanf("%d", a+i);
bool res = true;
for(int i=n; i>=1; i--)
{
if(a[i] > n-i) {res = false;break;};
Insert(a[i], i);
}
if(!res) puts("No solution");
else{
InOrder(root);
int size = ans.size();
for(int i=0; i<size; i++)
printf("%d%s", ans[i], i==size-1?"\n":" ");
}
}
return 0;
}