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... 这题就是利用二进制来标记颜色的种 ...
随机推荐
- dom4j的用法
package xml; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; impor ...
- Bean-Query 一个把对象转换为Map的Java工具库
刚开源了一个经过完整測试的Java工具类. 地址例如以下: https://github.com/Jimmy-Shi/bean-query 使用说明例如以下: Bean-query Click Her ...
- hdu4521(线段树+dp)
传送门:小明系列问题——小明序列 题意:有n个数,求间距大于d的最长上升序列. 分析:dp[i]表示在i点以a[i]结束距离大于d的最长上升序列,然后每更新到第i点时,取i-d之前小于a[i]的数为结 ...
- WPF案例 (六) 动态切换UI布局
原文:WPF案例 (六) 动态切换UI布局 这个Wpf示例对同一个界面支持以ListView或者CardView的布局方式呈现界面,使用控件ItemsControl绑定数据源,使用DataTempla ...
- filezilla安装
[alexus@wcmisdlin02 bin]$ ./filezilla ./filezilla: /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not ...
- CC Subarray LCM (数学)
题目连接:http://www.codechef.com/problems/SUBLCM 题意:给定一个序列,求最长连续子序列满足 LCM(Ai,Ai+1...Aj) =Ai*Ai+1*...*Aj. ...
- cocos2d-x 贝塞尔曲线的简单运用(CCBezierTo,CCBezierBy)
原文链接:http://blog.csdn.net/we000636/article/details/8616355 一.贝赛尔曲线简单介绍 贝塞尔曲线是应用于二维图形应用程序的数学曲线.曲线的定义有 ...
- 2-13. 平均两个有序序列(25)(ZJU_PAT 名单 | 排列 )
主题链接:http://pat.zju.edu.cn/contests/ds/2-13 已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数.有序序列A0, A1-AN-1的中位 ...
- redis作为mysql的缓存服务器(读写分离) (转)
一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录 ...
- IIS设置允许下载.exe文件解决方法
最近很多客户使用IIS服务器,然后提示返现宝下载无法找到等无法下载的问题. 返现宝是.exe安装文件,部分服务器或主机可能无法下载. 第一.如果是自己服务器或VPS请按如下设置: 1.设置MIME,让 ...


