Lost Cows(入门题)

Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 6585
Accepted: 4179

Description

N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.

Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.

Given this data, tell FJ the exact ordering of the cows.

Input

* Line 1: A single integer, N

* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.

Output

* Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.

Sample Input

5
1
2
1
0

Sample Output

2
4
5
3
1

Source

USACO 2003 U S Open Orange

这是我的第一道线段树,虽然是参考别人做的,但我会进一步好好品味线段树的精髓所在
首先对这道题目进行分析:
输入为:5 1 2 1 0
输出为:2 4 5 3 1
5代表了有5只牛,编号分别为1 2 3 4 5
首先从最后读起,最后一个数为0,即说明没有它的前面没有比它大的牌号了,那么这时去掉1,剩下的有2 3 4 5
接着读倒数第二位1,说明在剩下的2 3 4 5中有一个牌号比它小,那么这时去掉3,剩下2 4 5
接着读倒数第三位2,说明在剩下的2 4 5中有两个牌号比它小,这时去掉5,剩下2 4
接着读倒数第四位1,说明在剩下的2 4中有一个牌号比它小,这时去掉4,剩下2
那么最后去掉2
那么这道题就可以运用线段树的思想,在1到5之间建立一个满二叉树,并且把每个线段树的长度也给记录下来,判断某位置的数是否处于某段线段树之间,最后确定出该位置代表的哪个数。


#include <iostream>
#include <cstdio>
#define MAXN 8001
using namespace std;
struct Tnode
{
  int l,r,len;
}tree[MAXN*4];
int a[MAXN],b[MAXN];

//建立线段树,常规的做法。
void build(int root,int left,int right)
{
  tree[root].l=left;
  tree[root].r=right;
  tree[root].len=right-left+1;
  if(left==right)
    return ;
  int mid=(left+right)/2;
  build(root*2,left,mid);
  build(root*2+1,mid+1,right);
}

//查找线段树
int query(int root,int t)
{
  tree[root].len--;

  if(tree[root].l==tree[root].r)//如果左右孩子都一样的话,则返回。
    return tree[root].l;

  else if(t<=tree[root*2].len)//如果t小于len的话,说明该数位于左孩子
    return query(root*2,t);

  else
    return query(root*2+1,t-tree[root*2].len);//如果t大于len的话,说明该数位于由孩子
}


int main()
{
  int n;
  scanf("%d",&n);
  for(int i=1;i<=n-1;i++)
    {
      scanf("%d",&a[i]);
    }
  build(1,1,n);
  for(int i=n-1;i>=1;i--)
    {
      b[i]=query(1,a[i]+1);
    }
  b[0]=query(1,0+1);
  for(int i=0;i<n;i++)
    {
      printf("%d\n",b[i]);
    }
  return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值