线段树

Problem I: Inversion Sequence

Time Limit: 2 Sec   Memory Limit: 256 MB
Submit: 83   Solved: 22
[ Submit][ Status][ Web Board]

Description

For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence which are prior to j and greater to j at the same time. The sequence a1, a2, a3, … , aN is referred to as the inversion sequence of the original sequence (i1, i2, i3, … , iN). For example, sequence 1, 2, 0, 1, 0 is the inversion sequence of sequence 3, 1, 5, 2, 4. Your task is to find a full permutation of 1~N that is an original sequence of a given inversion sequence. If there is no permutation meets the conditions please output “No solution”.

Input

There are several test cases.
Each test case contains 1 positive integers N in the first line.(1 ≤ N ≤ 10000).
Followed in the next line is an inversion sequence a1, a2, a3, … , aN (0 ≤ aj < N)
The input will finish with the end of file.

Output

For each case, please output the permutation of 1~N in one line. If there is no permutation meets the conditions, please output “No solution”.

Sample Input

5
1 2 0 1 0
3
0 0 0
2
1 1

Sample Output

3 1 5 2 4
1 2 3
No solution

HINT

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-6;
const int MS=10005;

int num[MS];
int node[4*MS];
bool flag;

int n;
int leaf;

void init()
{
      leaf=1;
      flag=true;
      while(leaf<=n+1)
            leaf<<=1;
      memset(node,0,sizeof(node));
      for(int i=leaf+1;i<=leaf+n;i++)
            node[i]=1;
      for(int i=leaf-1;i>0;i--)
            node[i]+=node[i<<1]+node[i<<1|1];
}

int  insert(int pos)
{
      int p=1;
      while(p<leaf)
      {
            node[p]--;
            if(node[p]<0)
            {
                  flag=false;
                  return 1;
            }
            if(node[p<<1]-pos>0)
                  p=p+p;
            else
            {
                  pos-=node[p<<1];
                  p=2*p+1;
            }
      }
      node[p]=0;
      return p-leaf;
}

int main()
{
      while(scanf("%d",&n)!=EOF)
      {
            init();
            int x;
            for(int i=1;i<=n;i++)
            {
                  scanf("%d",&x);
                  if(flag)
                        num[insert(x)]=i;
            }
           if(flag==false)
                  printf("No solution");
           else
                  for(int i=1;i<=n;i++)
           {
                 if(i>1)
                        printf(" ");
                 printf("%d",num[i]);
           }
           printf("\n");
      }
      return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值