[CSP] strange race simulation queue (Fenwick tree & half & greed)

Title Description

too much nodgd fans, every day many people lined up for autographs.
Today there are n people line up, each person's height is an integer, and different from each other. Very unfortunately, I nodgd busy with other things today to go, they had to let these fans come back tomorrow. Meanwhile nodgd made a request, each person must remember that in front of their own and the number is higher than their own people in order to restore order today to tomorrow.
However, some fans are more or less disappointed, disappointed to enable them confused and disoriented, fascinated, already could not tell which side is the "front", so they probably remember the number in front of their own people than , it may be to remember the number of the back of higher than themselves, and they do not know is to remember which direction.
nodgd think, even so tomorrow can restore order in a queue, so that the two directions in any one of at least the number is higher than his one direction and he remembered the same figure. Unfortunately, n is relatively large, a clear need to write a program to solve, nodgd busy writing programs this kind of thing on to you.

Input Format

The first line of the input integer n, the number of instructions. (n <= 100000, in the height range integer)
Next n lines of two integers ai, bi, it represents a person's height and she remembered number ensure mutually different height.

Output Format

Output line, the queue in front of everyone to the height. If there are multiple answers to questions intended to meet the minimum output lexicographically. If you meet the meaning of the title arrangement, the output "impossible" does not exist

SAMPLE INPUT

4
4 1
3 1
6 0
2 0

Sample Output

2436
prompts
the answer given queue, the height of the first individual 2, individual higher than 0 preceded him, so he is the fourth individual input; a second height of four individual right individual 1 taller than him, so he was the first individual input; third height of 3, on the right there is one person than he, so he was the first two individuals entered; fourth person height of 6, 0 left than his personal high, so he is the third individual input.
Obviously, if the arrangement is "6342" is also to meet the meaning of the title, but not the smallest lexicographically

analysis

  The idea of ​​the untold problems, but I'm going to force the mouth Hu Yibo

  Consider inserted from small to large, as much as possible because it can control the greedy lexicographically smallest upon insertion, i.e., the current man tries to put forward.

  We first pre-out for everyone, how many individuals taller than her, if she is higher than the number of persons less than the number she remembered no solution.

  Then possible to calculate the minimum number of individuals in front of her than she is assumed to be the k

  So when she inserted because the insertion of small to large, so it should be reserved in front of k-space than to her, she is in the first k + 1 vacancies at

  This will not only ensure a solution, we can also guarantee a minimum lexicographical order.

  However, if the same level of human existence, may be reserved in front of more than k spaces, because the back of the insert may occupy a space not contribute

  That is, if two people of the same height, k older people inserted first, after inserting the smaller k, k smaller people who would destroy a large k out of space reserved

  As shown, the height of the No. 1 and No. 2 are equal, k k = 1 No. 2 No. = 5,2

 

 

  No. 1 to taller than her man left a vacancy when inserting the k

 

 

 

  But when No. 2 insert will occupy a space not contribute

  The first insert 2 insert 1 does not exist a problem

 

 

 

  So, when you encounter the same height by k small to large order after inserting it.

  So the question is, how do we achieve it?

  We can use an array to maintain the tree

  Each space is 1, so the prefix is ​​a location and this location before and the total number of vacancies

  Obviously the right prefix and is a single growing, it is possible to determine the k-th binary positions vacancies

  When a person is inserted, the first two divided into a first slot k + 1 position, the answer records, and then modify the value of the position of the array in the tree, the next person is inserted again

  复杂度为O(nlog^2n)。好像树状数组还有nlogn的做法,但我想咕了。

  代码

#include<cstdio>
#include<algorithm>
using namespace std;
int n,vis[100005],bit[100005],ans[100005];
struct node{int h,id;}st[100005];
void add(int x,int k){for(;x<=n;x+=(x&-x))bit[x]+=k;}
bool cmp(node a,node b){return a.h==b.h?a.id<b.id:a.h<b.h;}
int que(int x){int s=0;for(;x;x-=(x&-x))s+=bit[x];return s;}
int main()
{
    scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d%d",&st[i].h,&st[i].id);
    sort(st+1,st+1+n,cmp);int flag=0;
    for(int i=1,l,r;i<=n;add(i,1),i++)
    {
        l=r=i;while(st[i].h==st[i+1].h)i++,r++;
        for(int j=l;j<=r;j++)flag|=(st[j].id=min(n-r-st[j].id,st[j].id))<0;
    }
    if(flag)return puts("impossible"),0;sort(st+1,st+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
        int l=1,r=n,mid,pos;
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(que(mid)>=st[i].id+1)r=mid-1,pos=mid;
            else l=mid+1;
        }
        add(pos,-1);
        ans[pos]=st[i].h;
    }
    for(int i=1;i<=n;i++)printf("%d%c",ans[i],i==n?'\n':' ');
}

 

Guess you like

Origin www.cnblogs.com/firecrazy/p/11654827.html