poj 2182 Lost Cows(段树精英赛的冠军)
主题链接:http://poj.org/problem?
id=2182
|
Lost Cows
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 Sample Output 2 Source |
[Submit] [Go Back] [Status]
[Discuss]
线段树的一道经典题目。这个题目能够转化成从后向前依次查询,比方当前奶牛的前面有x个号码比它小的奶牛,那么它就应该在剩余的数的序列中排第x+1。当某个号码确定时,就在线段树中去除这个号码。我们遍历一下线段树若左子树区间内未删除元素个数满足当前要找的数成为第a+1个。能则递归左子树。否则递归右子树,直至到叶子节点。那么叶子节点的值就是其初始编号。
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<vector>
#include<bitset>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<cstdlib>
using namespace std;
#define CLR(A) memset(A,0,sizeof(A))
const int MAX=8010;
struct Node{
int l,r,len,v;
}tree[MAX*4];
int ans[MAX];
int f[MAX];
inline void pushup(int id){
tree[id].len=tree[id<<1].len+tree[id<<1|1].len;
}
void build(int id,int l,int r){
tree[id].l=l;tree[id].r=r;
if(l==r){
tree[id].v=l;
tree[id].len=1;
return;
}
int m=(l+r)>>1;
build(id<<1,l,m);
build(id<<1|1,m+1,r);
pushup(id);
}
void query(int id,int cur,int pos){
if(tree[id].l==tree[id].r){
tree[id].len=0;
ans[pos]=tree[id].v;
return;
}
if(tree[id<<1].len>=cur) query(id<<1,cur,pos);
else query(id<<1|1,cur-tree[id<<1].len,pos);
pushup(id);
}
int main(){
int n;
while(~scanf("%d",&n)&&n){
for(int i=2;i<=n;i++){
scanf("%d",&f[i]);
f[i]++;
}
build(1,1,n);
f[1]=1;
for(int i=n;i>=1;i--){
query(1,f[i],i);
}
for(int i=1;i<=n;i++){
printf("%d\n",ans[i]);
}
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
poj 2182 Lost Cows(段树精英赛的冠军)的更多相关文章
- POJ 2182 Lost Cows 【树状数组+二分】
题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 2182 Lost Cows (线段树)
题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ...
- POJ 2182/暴力/BIT/线段树
POJ 2182 暴力 /* 题意: 一个带有权值[1,n]的序列,给出每个数的前面比该数小的数的个数,当然比一个数前面比第一个数小的个数是0,省略不写,求真正的序列.(拗口) 首先想到的是从前到后暴 ...
- POJ 2182 Lost Cows(牛排序,线段树)
Language: Default Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9207 Acce ...
- 线段树/树状数组 POJ 2182 Lost Cows
题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号: ...
- POJ 2182 Lost Cows (树状数组 && 二分查找)
题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...
- POJ 3264-Balanced Lineup(段树:单点更新,间隔查询)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34522 Accepted: 16224 ...
- POJ 2182 Lost Cows
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10996 Accepted: 7059 Description N (2 ...
- POJ 2777 Count Color(段树)
职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...
随机推荐
- 简单的三方登录SDK示例,Android Activity之间数据的传递
先建立Library工程,即普通工程然后在Android的属性勾选Library选项. 这里建立的工程为 mySdk ,Activity名为LoginActivity. LoginActivity代码 ...
- Android 的Google+平台
Google+是谷歌推出的身份服务和社交网站.也是Google各种服务社交层面的补强.是世界上第二大的社交网站.一旦用户登录到Google,你就可以按照自己的需要定制服务和使用你的应用程序.
- javascritp封装的类似java HashMap的类
function Map(){ this.init(); }; //map的大小 Map.prototype.size = function(){ return this._size; }; //将数 ...
- android4.3环境搭建
方案一: 首先android环境搭建有如下几个东西是必须准备的: 1. Eclipse (下载地址:http://www.eclipse.org/downloads/,建议至少3.4及以上版本) 2 ...
- iOS 同步GET
(注意: 能够整片复制)
- myeclipse10.7皴,出口解决war包时报“SECURITY ALERT: INTEGERITY CHECK ERROR”
一.操作系统的环境是win7,64bit和32bit的操作系统我试过都OK 依照网上一些Crack破解程序步骤操作就能够完毕破解过程, 也能够到我的CSDN资源里下载文件包 myeclipse10.7 ...
- (转)ikvmc的使用
IKVM.NET是一个针对Mono和微软.net框架的java实现,其设计目的是在.NET平台上运行java程序.本文将比较详细的介绍这个工具的原理.使用入门(如何java应用转换为.NET应用.), ...
- 【解决方法】ADT在线安装
作为android开发者,不知道被ADT更新.SDK更新这些更新给强奸了多少次. 今天……我又一次被凌辱了. 无论是 https://dl-ssl.google.com/android/eclipse ...
- [置顶] JQuery实战总结三 标签页效果图实现
在浏览网站时我们会看到当我们鼠标移到多个选项卡上时,不同的选项卡会出现自己对应的界面的要求,在同一个界面上表达了尽量多的信息.大大额提高了空间的利用率.界面的切换效果也是不错的哦,这次自己可以实现啦. ...
- 使用JS模拟出Map对象
近期要做的一个项目,支持方提供的一个Map方法,用着相当能够,功能稍有欠缺,因此我为之做了扩展,下面是代码: function Map() { this.elements = new Array(); ...


