NOJ 1317——Exercise of Sort

  • 问题描述
  • Wodex opened the door successfully. 
    At the same time, horn sounded: 
    If you are not clever, the game will be so boring, next level will be more difficult. Ha-ha-ha... 

    So, Wodex came in with his partners. 
    At this room, there was a ciphertext on the wall. 
    The words means: 
    There are N lines, each line has two integers i and x, it means value x will be put on ith position. Every x is different. 

    How to form the sequence of these values? 

  • 输入
  • Input until EOF. 
    First line will contain an integer N (1 <= N <= 200,000), then N lines follow, each line has two integers i (0 <= i < N) and x (0 <= x <= 1,000,000). 
  • 输出
  • Output the final sequence of these numbers. 
  • 样例输入
  • 4
    0 77
    1 51
    1 33
    2 69
    4
    0 20523
    1 19243
    1 3890
    0 31492
  • 样例输出
  • 77 33 69 51
    31492 20523 3890 19243
  • 提示
  • 来源
  • Hungar

线段树完成,最后一个数的位置一定是确定的。具体见http://hainingqtj.blog.163.com/blog/static/23349311920146411520361/

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iostream>

using namespace std;

const int maxn = 200010;

int tree[maxn<<2];

int _sort[maxn];

struct node
{
int val,pos;
}num[maxn];

void build(int p,int l,int r)
{
tree[p]=r-l+1;
if(l == r)
return;
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}

void update(int p,int l,int r,int val,int pos)
{
tree[p]--;
if(l == r)
{
_sort[l]=val;
return ;
}
int mid=(l+r)>>1;
if(tree[p<<1] >= pos)
update(p<<1,l,mid,val,pos);
else
{
pos-=tree[p<<1];
update(p<<1|1,mid+1,r,val,pos);
}

}

int main()
{
int n;
while(~scanf("%d",&n))
{
build(1,1,n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&num[i].pos ,&num[i].val);
}
for (int i = n-1; i >= 0; --i)
{
update(1,1,n,num[i].val,num[i].pos+1);
}
for (int i = 1; i <= n; ++i)
{
printf("%d",_sort[i]);
if(i<n)
printf(" ");
}
printf("\n");
}
return 0;
}


你可能感兴趣的:(NOJ 1317——Exercise of Sort)