POJ2182 HDU2711 Lost Cows

60 篇文章 0 订阅
12 篇文章 0 订阅

用线段树来做, 我们来看看实例数据

0 1 2 1 0  (关于第一个0请看题目要求)


从后面往前插. 

0, 在1, 2, 3, 4, 5中第0个,即1

1, 在2, 3, 4, 5中第1个,即3

2, 在2, 4, 5中第2个, 即5

1, 在2, 4中第1个,即4

0, 在2中第0个, 即2


故可以用线段树来做. NODE中的val指的是节点中[l,r)空余的位置.


/*******************************************************************************
 # Author : Neo Fung
 # Email : neosfung@gmail.com
 # Last modified: 2012-01-30 20:20
 # Filename: POJ2182 HDU2711 Lost Cows.cpp
 # Description : 
 ******************************************************************************/
#ifdef _MSC_VER
#define DEBUG
#endif

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <limits.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <functional>
#include <ctype.h>
#include <stack>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MAX 8010
using namespace std;

struct NODE
{
	int l,r,val;
}node[MAX*4];

stack<int> stk;

void init()
{
	memset(node,'\0',sizeof(node));
	while(!stk.empty())
		stk.pop();
}

void build(const int &t , const int &l,const int &r)
{
	node[t].l=l;
	node[t].r=r;
	node[t].val=r-l;
  if(l==r-1)
    return;
	int mid=(l+r)>>1;
	build(L(t),l,mid);
	build(R(t),mid,r);
}

void update(const int &t,const int &val)
{
  --node[t].val;
	if(node[t].l == node[t].r-1)
  {
	  stk.push(node[t].l);
		return;
  }
	if(node[L(t)].val>val)
		update(L(t),val);
	else
		update(R(t),val-node[L(t)].val);
}

int main(void)
{
#ifdef DEBUG  
  freopen("../stdin.txt","r",stdin);
  freopen("../stdout.txt","w",stdout); 
#endif  

  int num[MAX],n;

  while(~scanf("%d",&n))
  {
	  num[0]=0;
	  for(int i=1;i<n;++i)
		  scanf("%d",&num[i]);
	  init();
	  build(1,0,n);
	  for(int i=n-1;i>=0;--i)
		  update(1,num[i]);
	  while(!stk.empty())
	  {
		  printf("%d\n",stk.top()+1);
		  stk.pop();
	  }
  }

  return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值