POJ 2182

二分+树状数组水过了,从后往前确定数字即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string.h>
#include <queue>
#include <cmath>
#include <vector>
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N=8010;

int num[N],n;
int order[N];
int ans[N];

void update(int x,int w){
  for(;x<=n;x+=lowbit(x))	num[x]+=w;
}

int sum(int x){
  int s=0;
  for(;x;x-=lowbit(x))	s+=num[x];
  return s;
}

int find(int s){
	int l=0,r=n,m;
	int ret;
	while(l<=r){
		m=(l+r)/2;
		if(sum(m)>=s){
			ret=m;
			r=m-1;
		}
		else l=m+1;
	}
	return ret;
}

int main(){
	int pos;
	while(scanf("%d",&n)!=EOF){
		for(int i=1;i<=n;i++)
		num[i]=0;
		for(int i=1;i<=n;i++)
		update(i,1);
		for(int i=2;i<=n;i++){
			scanf("%d",&order[i]);
		}
		order[1]=0;
		for(int i=n;i>=1;i--){
			pos=find(order[i]+1);
			ans[i]=pos;
			update(pos,-1);
		}
		for(int i=1;i<=n;i++)
		printf("%d\n",ans[i]);
	}
	return 0;
}

  

时间: 2024-08-06 20:08:24

POJ 2182的相关文章

POJ 2182 Lost Cows.(线段树)

~~~~ 数据太水,暴力无压力,不过还是用线段树写了下,表现了楼主对线段树的无限热爱之情. ~~~~ 题目连接:http://poj.org/problem?id=2182 大致题意;牛牛因XXXXXX原因乱序成一排,现已知每头牛前面有多少头牛比它的编号小(第一头牛前面没有当然就不列粗来了),求每头牛的编号. 思路:从后往前扫描,遇到a,则说明它是剩余序列的第a+1头牛. ~~~~ 暴力代码:(157ms) #include<cstdio> #include<algorithm>

poj 2182 Lost Cows (线段树)

 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8838   Accepted: 5657 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 'wate

poj 2182 Lost Cows(线段树经典题)

题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9152   Accepted: 5879 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, th

Lost Cows POJ 2182 思维+巧法

Lost Cows POJ 2182 思维 题意 是说有n头牛,它们身高不一但是排成了一队,从左到右编号为1到n,现在告诉你从第二号开始前面的那些牛中身高小于它的个数,一共有n-1个数.然后求出它们按照身高来排序的话从低到高编号会是多少. 解题思路 首先我们需要从它给的数据逆序来进行处理,为什么,比如倒数第一个数据是0的话,说明前面没有比它矮的牛,那么它的编号就是1,然后倒数第二个是2的话,就是说前面有两个比它矮,因为1号已经有了,所以他就是4号,后面以此类推. 根据这个思路我们就可以进行模拟(

POJ 2182 线段树

链接: http://poj.org/problem?id=2182 题意: 有N头牛,编号1~N,乱序排成一列,现在已知每头牛前面有多少头牛比它的编号小, 求排队后从前往后数,每头牛的编号 题解: 从后往前扫描,遇到数字a,说明它是剩余序列中的第a+1个数,找到改编号并删除. 代码: 31 int a[MAXN]; 32 int tree[MAXN]; 33 34 void pushup(int rt) { 35 tree[rt] = tree[rt << 1] + tree[rt <

poj 2182 Lost Cows

Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9536   Accepted: 6146 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 'waterin

线段树/树状数组 POJ 2182 Lost Cows

题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号:为1,倒数第二头牛的编号为除去最后一头牛的编号后的第a[i-1] + 1编号:为3,其他的类推,所以可以维护之前已经选掉的编号,求第k大的数字,sum[rt] 表示该区间已经被选掉的点的个数.另外树状数组也可以做,只不过用二分优化查找第k大的位置. 收获:逆向思维,求动态第K大 代码(线段树): /

POJ 2182 Lost Cows(牛排序,线段树)

Language: Default Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9207   Accepted: 5922 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 nei

POJ 2182 Lost Cows 巧妙解法

题目 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11766   Accepted: 7570 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 'wat