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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值