POJ 2182 Lost Cows(牛排序,线段树)
Language:
Default
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 |
给牛排序,n头牛,然后从第二头牛開始给出他在比他序号小这么多牛的地位,最后确定牛的排名
第一次看别人题解惊呆了,f[pos].va--,竟然就能够把这个位置腾出来,以后都不会訪问,另一点,从后往前给牛排名
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define N 8005 struct stud{
int le,ri;
int len;
}f[N*4]; int a[N];
int ans[N]; void build(int pos,int le,int ri)
{
f[pos].le=le;
f[pos].ri=ri;
f[pos].len=ri-le+1;
if(ri==le) return ; int mid=MID(le,ri); build(L(pos),le,mid);
build(R(pos),mid+1,ri); } int query(int pos,int le)
{
f[pos].len--; if(f[pos].le==f[pos].ri)
return f[pos].le; if(f[L(pos)].len>=le)
return query(L(pos),le);
return query(R(pos),le-f[L(pos)].len);
} int main()
{
int i,j,n;
while(~scanf("%d",&n))
{
build(1,1,n); for(i=2;i<=n;i++)
scanf("%d",&a[i]); a[1]=0; for(i=n;i>=1;i--)
ans[i]=query(1,a[i]+1); for(i=1;i<=n;i++)
printf("%d\n",ans[i]); return 0;
}
return 0;
}
POJ 2182 Lost Cows(牛排序,线段树)的更多相关文章
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- 【POJ 2750】 Potted Flower(线段树套dp)
[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4566 ...
- POJ 2182 Lost Cows (线段树)
题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ...
- 线段树/树状数组 POJ 2182 Lost Cows
题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号: ...
- poj 2182 Lost Cows(段树精英赛的冠军)
主题链接:http://poj.org/problem? id=2182 Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- 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, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...
- POJ - 3264——Balanced Lineup(入门线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 68466 Accepted: 31752 ...
随机推荐
- 手动配置S2SH三大框架报错(三)
十二月 08, 2013 10:24:43 下午 org.apache.catalina.core.AprLifecycleListener init 严重: An incompatible vers ...
- React-TodoList
React入门最好的学习实例-TodoList 前言 React 的核心思想是:封装组件,各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件. 最近前端界闹的沸沸扬扬的技术当属react ...
- javascript实现图片无缝滚动(scrollLeft的使用方法介绍)
<!DOCTYPE html > <html> <head> <meta http-equiv="Content-Type" conten ...
- 代码静态分析工具PC-LINT安装配置
代码静态分析工具PC-LINT安装配置--step by step 作者:ehui928 ...
- haproxy配置监控redis主备切换(转)
环境前提: redis sentinel配置,三台主机,且配置运行良好 配置文件中添加: frontend ft_redis bind 0.0.0.0:6379 name re ...
- linux串口编程总结
串口本身.标准和硬件 † 串口是计算机上的串行通讯的物理接口.计算机历史上,串口以前被广泛用于连接计算机和终端设备和各种外部设备.尽管以太网接口和USB接口也是以一个串行流进行数据传送的.可是串口连接 ...
- Android开发经验之—intent传递大数据
在Activity或者组件之前传递信息时,一般採用intent绑定bundle的方式传值,但在使用过程中须要注意的是不要用bundle传递大容量数据: 在做项目的过程中,须要将听写界面的听写结果信息传 ...
- Latin1的所有字符编码
ISO-8859-1 (ISO Latin 1) Character Encoding Contents The characters at a glance Character codes and ...
- (Google面试题)有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。
现要让四个文件呈如下格式: A:1 2 3 4 1 2.... B:2 3 4 1 2 3.... C:3 4 1 2 3 4.... D:4 1 2 3 4 1.... 请设计程序. 下面举例A,对 ...
- TStack,TQueue,TObjectList,TObjectStack等等
TStack,TQueue,TObjectList,TObjectStack等等,都在Contnrs.pas单元里,需要手动添加. 不同于TList类,TObjectList对象将销毁任何从列表中删除 ...