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... 这题就是利用二进制来标记颜色的种 ...
随机推荐
- nginx 源码安装
安装环境: 操作系统:Ubuntu 12.04 Nginx: V1.4.2 PCRE: V8.33 zlib: V1.2.8 下载上述源包到当前用户主目录(本机:/hom ...
- [置顶] vs2008 编译adb 支持4.2 android 系统(增加push 命令的进度)
QQ: 2506314894 本想晚些时候放出来的,但是按捺不住啊,所以修改了之后就立即放出来了.先说明一下,这次用的adb 的源码比较新的,用的vs2008 编译出来,只有一个exe 文件,直接就可 ...
- VSTO学习笔记(三) 开发Office 2010 64位COM加载项
原文:VSTO学习笔记(三) 开发Office 2010 64位COM加载项 一.加载项简介 Office提供了多种用于扩展Office应用程序功能的模式,常见的有: 1.Office 自动化程序(A ...
- redis优化配置和redis.conf说明
1. redis.conf 配置參数: #是否作为守护进程执行 daemonize yes #如以后台进程执行,则需指定一个pid,默觉得/var/run/redis.pid pidfile redi ...
- Java设置的读书笔记和集合框架Collection API
一个.CollectionAPI 集合是一系列对象的聚集(Collection). 集合在程序设计中是一种重要的数据接口.Java中提供了有关集合的类库称为CollectionAPI. 集合实际上是用 ...
- xcode project
An Xcode project is a repository for all the files, resources, and information required to build one ...
- Python用Tkinter的Frame实现眼睛护士的倒计时黑色屏幕
import Tkinter,time class MyFrame(Tkinter.Frame): def __init__(self): Tkinter.Frame.__init__(self) s ...
- 关于静态与动态编译arm平台程序的比較
因为近期弄个console程序,调用了readline,ncurses库,这两个动态库加起来有四百多k.而程序事实上非常小,其它地方也没使用到这两个库 所以想静态编译看看console程序有多大. # ...
- IL来理解属性
IL来理解属性 阅读目录 概述: C#中如何定义一个属性 Student类 属性Name Main方法 实现get,set方法 性能 访问权限 回到最开始提出的问题 参考资料 .Net底层剖析目录 ...
- SharedPreferences共享优先存储的详细解析和原理
共享优先存储: publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCont ...


