POJ 2182 Lost Cows
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10996 | Accepted: 7059 |
Description
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
* 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
Sample Input
5
1
2
1
0
Sample Output
2
4
5
3
1
Source
另开一个数组按编号顺序记录奶牛的状态,像这样: 1 1 1 1 1
算前缀和就能知道一头牛前面的牛数量。
倒着处理数据,若最后一头牛看到前面有0头编号更小的牛,通过上面的数组就可以知道这头牛的编号是1。
记录最后一头牛的编号,然后在数组中删去这头牛,数组变成0 1 1 1 1
以此类推
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int a[mxn];
int c[mxn];
int num[mxn];
int n;
inline int lowbit(int x){
return x&-x;
}
inline int sum(int x){
int i=x,res=;
while(i){
res+=c[i];
i-=lowbit(i);
}
// printf("sum[%d]==%d\n",x,res);
return res;
}
inline int find(int x){
int l=,r=n;
int mid;
while(l<r){
mid=((l+r)>>)+;
if(sum(mid)<=x)l=mid;
else r=mid-;
}
return l;
}
int main(){
scanf("%d",&n);
int i,j;
for(i=;i<n;i++){
scanf("%d",&a[i+]);
}
for(i=;i<=n;i++){//初始化
int tmp=i;
while(tmp<=n){
c[tmp]++;
tmp+=lowbit(tmp);
}
}
for(i=n;i>=;i--){
int tmp=find(a[i]+);
// printf("test %d %d\n",a[i],tmp);
num[i]=tmp++;
while(tmp<=n){
c[tmp]--;
tmp+=lowbit(tmp);
}
}
for(i=;i<=n;i++)printf("%d\n",num[i]);
return ;
}
POJ 2182 Lost Cows的更多相关文章
- 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(牛排序,线段树)
Language: Default Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9207 Acce ...
- POJ 2182 Lost Cows (线段树)
题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ...
- POJ 2182 Lost Cows (求序列第k大)
题解 二分+树状数组 显然最和一个数的值就是rank 那么其它数有什么规律? 从后往前匹配rank,我们可以发现第i个数的rank为还没有匹配的rank第(a[i]+1)大的数 这可以用 树状数组+二 ...
- 线段树/树状数组 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 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...
- Lost Cows POJ 2182 思维+巧法
Lost Cows POJ 2182 思维 题意 是说有n头牛,它们身高不一但是排成了一队,从左到右编号为1到n,现在告诉你从第二号开始前面的那些牛中身高小于它的个数,一共有n-1个数.然后求出它们按 ...
- POJ 2182/暴力/BIT/线段树
POJ 2182 暴力 /* 题意: 一个带有权值[1,n]的序列,给出每个数的前面比该数小的数的个数,当然比一个数前面比第一个数小的个数是0,省略不写,求真正的序列.(拗口) 首先想到的是从前到后暴 ...
随机推荐
- java22 - 1 多线程之 单线程和多线程的图解
- 富有表现力的javascript
1.javascript的灵活性,你可以把它写的很简单,也可以写的很复杂,简直就是随心所欲: 2.javascript是弱类型语言,定义变量的时候不用声明变量类型,不声明类型,并不是说,javascr ...
- 图解Js event对象offsetX, clientX, pageX, screenX, layerX, x区别
通过 3 张图和 1 张表格,轻松区别 JavaScript Event 对象中的offsetX, clientX, pageX, screenX, layerX, x等属性. 一.测试代码如下: & ...
- 1从零开始学习Xamarin.iOS安装篇
安装和配置xamarin.ios 最近.net 开源新闻很火呀,于是想学习xamarin,早1年前就了解过这个东西,但是一直没有时间来学习,我这里装的是MAC上面的版本,废话不多说开始第一步安装. 概 ...
- Google java代码风格导入Eclipse
Git地址 https://github.com/codeset/google-java-styleguide 下载配置文件在Eclipse中执行导入:Window -> Preferences ...
- poj2154-color-polyan次二面体+欧拉函数优化
N<=1e9,O(nlogn)的做法会超时.从枚举置换转变为枚举轮换长度,然后可以利用欧拉函数,把复杂度变为O(√n * logn) /*---------------------------- ...
- unity3d 扩展NGUI Tweener —— TweenFillAmount
好久没写博客了,上一篇是在今年上班之前写的 从年初到现在一篇没写过,每天都在加班,实在太忙了 上班半年多了,学到不少东西 今天分享一下刚写的小功能 TweenFillAmount 用过NGUI Twn ...
- 原生JS、CSS实现的转盘效果(目前仅支持webkit)
这是一个原生JS.CSS实现的转盘效果(题目在这:http://www.cnblogs.com/arfeizhang/p/turntable.html),花了半个小时左右,准备睡觉,所以先贴一段代码, ...
- Ruby On Rails 常用的精品Gem汇总
首先需要注明一点,本文是原创的并不是从其它地方转载.所有的数据是我从 GitHub 和 RubyGems 上码下来的,数据的截取时间就是本文的发布日期. RubyGems 的下载量可以看到在用这个 g ...
- Windows下apache php wordpress配置
2. Use notepad to open httpd.conf config file. Make use the line "LoadModule rewrite_module mod ...