题意:给你一个序列a[i],代表 i这个数 在b数列中有多少个值在它前面且比它大的个数,问你求B序列

解题思路:线段树的简单应用,找第几个空,类似二分。

解题代码:

  1 // File Name: i.cpp
  2 // Author: darkdream
  3 // Created Time: 2015年03月28日 星期六 12时56分11秒
  4 
  5 #include<vector>
  6 #include<list>
  7 #include<map>
  8 #include<set>
  9 #include<deque>
 10 #include<stack>
 11 #include<bitset>
 12 #include<algorithm>
 13 #include<functional>
 14 #include<numeric>
 15 #include<utility>
 16 #include<sstream>
 17 #include<iostream>
 18 #include<iomanip>
 19 #include<cstdio>
 20 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24 #define LL long long
 25 #define maxn 10005
 26 using namespace std;
 27 int a[maxn];
 28 struct node{
 29   int l ,r , m , v; 
 30 }tree[maxn*4];
 31 int ans[maxn];
 32 int L(int x)
 33 {
 34  return 2 * x; 
 35 }
 36 int R(int x)
 37 {
 38  return 2 *x + 1; 
 39 }
 40 void push_up(int c)
 41 {
 42     tree[c].v = tree[L(c)].v + tree[R(c)].v; 
 43 }
 44 void build(int c ,int l , int r)
 45 {
 46     tree[c].l = l; 
 47     tree[c].r = r ; 
 48     tree[c].m = (l + r)/2;
 49     if(tree[c].l ==tree[c].r)
 50     {
 51         tree[c].v = 1; 
 52         return;
 53     }
 54     build(L(c),l,tree[c].m);
 55     build(R(c),tree[c].m+1,r);
 56     push_up(c);
 57 }
 58 int tt ;
 59 void find(int c, int v)
 60 {
 61    if(tree[c].l == tree[c].r)
 62    {
 63       tt = tree[c].l ;
 64       tree[c].v = 0 ; 
 65       return ; 
 66    }
 67    if(tree[L(c)].v >= v)
 68    {
 69       find(L(c),v);
 70    }else{
 71       find(R(c),v-tree[L(c)].v);
 72    }
 73    push_up(c);
 74 }
 75 int ta;
 76 int main(){
 77   int n ;
 78   while(scanf("%d",&n) != EOF)
 79   {
 80     build(1,1,n);
 81     memset(ans,0,sizeof(ans));
 82     int ok = 0 ;
 83     for(int i = 1;i <= n; i ++)
 84     {
 85       scanf("%d",&ta);
 86       if(tree[1].v < ta+1)
 87       {
 88           ok = 1; 
 89           continue;;
 90       }
 91       if(ok == 0 )
 92       {
 93         find(1,ta+1);
 94         ans[tt] = i ; 
 95       }
 96     }
 97     if(ok == 1)
 98         printf("No solution\n");
 99     else {
100       for(int i = 1;i <= n;i ++)
101           printf(i == 1?"%d":" %d",ans[i]);
102       printf("\n");
103     }
104   }
105 return 0;
106 }
View Code

 

posted on 2015-03-29 09:38  dark_dream  阅读(169)  评论(0编辑  收藏  举报