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,省略不写,求真正的序列.(拗口) 首先想到的是从前到后暴 ...
随机推荐
- java 20 - 8 字节流的文件复制以及汉字在计算机中的存储方式
复制文本文件:把当前目录下的FileIntputStream.java文件里面的内容复制到当前目录的b.txt文件中 分析: 数据源: FileIntputStream.java -- 读取数据 -- ...
- Genymotion出现unknown generic error和This may occur if you are using a proxy错误的解决方案
今天在实验室希望在Genymotion上多下载几个模拟器,需要重新登录帐号,却发现一个错误,叫做unknown generic error.前几天还出现过一个很诡异的问题.截图如下: . (1)unk ...
- Mac上安装node.js
1.下载node for mac并一路默认安装 2.测试成功否 3.copy this file to test(save as javascript file) var http = require ...
- 【ArcGIS二次开发】CreateFeature报错(HRESULT E_FAIL)
在VS2010下基于ArcGIS 10.1做二次开发时遇到个奇怪的问题,对于MXD工程文档中已经存在的图层,获取其FeatureClass可以调用CreateFeature()函数生成要素,但是对于通 ...
- 如何等到所有的图片都加载完成之后触发一次onload事件
var details_img = $(".details img"); //所有的图片 var img_len = details_img.length; details_img ...
- 13Spring_AOP编程(AspectJ)_后置通知
后置通知和前置通知差不多.最大的特点是因为后置通知是运行在目标方法之后的,所以他可以拿到目标方法的运行的结果. 给出案例: 案例结构图:
- 第二章 时间控件(DateTime Picker)
这家伙太懒了,碰到问题才写博文,嘿嘿. 好了进入正题,二话不说,先放地址: 中文:http://www.bootcss.com/p/bootstrap-datetimepicker/index.htm ...
- 解析 HTTP(HttpURLConnection getResponseCode)
HTTP 请求 客户端通过发送 HTTP 请求向服务器请求对资源的访问.HTTP 请求由三部分组成,分别是:请求行.消息报头和请求征文. 3.1.请求行 请求行以一个方法符号开头,后面跟着请求 URI ...
- 简谈Java的join()方法
join()是Thread类的一个方法.根据jdk文档的定义: public final void join()throws InterruptedException: Waits for this ...
- IT客学院《构建高转化率的着陆页-PS+HTML+网络营销》共25节【价值199元】无水印版
课程简介本课程是全网独家专业的着陆页课程,课程完整的再现了整个着陆页实战案例的开发过程,包括:策划.设计和实现.上线后的推广.优化及提高转化率的技巧等,本套课程能帮助您迅速掌握着陆页的能力,迅速洞察完 ...