Lost Cows

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 10354 Accepted: 6631

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

1

2

1

0

Sample Output

2

4

5

3

1

Source

USACO 2003 U S Open Orange

类似排队买票,从后先前查找

#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <string>
#include <cstdio>
#include <iostream>
#include <algorithm> using namespace std; typedef long long LL; int Tree[32000]; int a[8500]; void Build(int L,int R,int site)
{
Tree[site]=(R-L+1);//每个区间中牛的个数 if(L==R)
{
return ;
}
int mid=(L+R)>>1; Build(L,mid,site<<1); Build(mid+1,R,site<<1|1);
} int Query(int L,int R,int site,int num)//查询牛的编号
{
Tree[site]--; if(L==R)
{ return L;
} int mid=(L+R)>>1; if(Tree[site<<1]>=num)
{
return Query(L,mid,site<<1,num);
}
else
{
return Query(mid+1,R,site<<1|1,num-Tree[site<<1]);
}
} int main()
{
int n; while(~scanf("%d",&n))
{
a[1]=0;//第一个元素前面比他小的牛自然是0个人 for(int i=2;i<=n;i++)
{
scanf("%d",&a[i]);
} Build(1,n,1); for(int i=n;i>=1;i--)
{
a[i]=Query(1,n,1,a[i]+1);//a[i]+1为这个牛的剩余牛中的位置
} for(int i=1;i<=n;i++)
{
printf("%d\n",a[i]);
}
}
return 0;
}

Lost Cows(线段树 POJ2182)的更多相关文章

  1. [poj2182] Lost Cows (线段树)

    线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...

  2. POJ 2481 Cows (线段树)

    Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每仅仅牛有一个值[S,E],假设对于牛i和牛j来说,它们的值满足以下的条件则证明牛i比牛j强壮:Si &l ...

  3. POJ 2182 Lost Cows (线段树)

    题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ...

  4. hdu 2711&&poj2182 Lost Cows (线段树)

    从后往前查第一个为0的奶牛肯定应该排在第一个.每次从后往前找到第一个为0的数,这个数应该插在第j位.查找之后,修改节点的值为极大值,当整棵树的最小值不为0的时候查找结束. 至于这种查找修改的操作,再没 ...

  5. poj2182(线段树求序列第k小)

    题目链接:https://vjudge.net/problem/POJ-2182 题意:有n头牛,从1..n编号,乱序排成一列,给出第2..n个牛其前面有多少比它编号小的个数,记为a[i],求该序列的 ...

  6. POJ2182题解——线段树

    POJ2182题解——线段树 2019-12-20 by juruoOIer 1.线段树简介(来源:百度百科) 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线 ...

  7. POJ 2182&& POJ 2828:Lost Cows 从后往前 线段树

    Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10544   Accepted: 6754 Descri ...

  8. ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)

    这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...

  9. POJ-2481 Cows (线段树单点更新)

    题目大意:给n个区间,对于任意一个区间,求出能完全包含它并且长度比它长的区间的个数. 题目分析:将一个区间视为二位坐标系中的一个点,左端点视作横坐标,右端点视作纵坐标.则题目变成了求每个点的左上方.正 ...

随机推荐

  1. WebApp 里Meta标签大全,webappmeta标签大全

    1.先说说mate标签里的viewport: viewport即可视区域,对于桌面浏览器而言,viewport指的就是除去所有工具栏.状态栏.滚动条等等之后用于看网页的区域.对于传统WEB页面来说,9 ...

  2. .net 实现Office文件预览,word文件在线预览、excel文件在线预览、ppt文件在线预览

    转自源地址:http://www.cnblogs.com/GodIsBoy/p/4009252.html,有部分改动 使用Microsoft的Office组件将文件转换为PDF格式文件,然后再使用pd ...

  3. oracle initialization or shutdown in progress问题解决步骤

        今天像往常一样打开电脑,启动plsql工具连接数据库,但是尽然连接不了,报了“oracle initialization or shutdown in progress”的提示信息,从操作系统 ...

  4. Oracle常见SQL语句

    --创建表空间--create tablespace ts_namedatafile 'd:\test\ts_name.dbf' size 100mautoextend on --调整表空间大小--a ...

  5. BizTalk开发系列(二十五) SQL Adapter

    SQL Server 是.NET开发的首选数据库.当然开发BizTalk应用程序很多也离不了SQL Server.针对SQL Server的数据操作BizTalk 提供了SQL Adapter作为与数 ...

  6. Print all nodes at distance k from a given node

    Given a binary tree, a target node in the binary tree, and an integer value k, print all the nodes t ...

  7. pod 新格式

    执行 $pod install 的时候,报一下错误: Analyzing dependencies [!] The dependency `MJExtension` is not used in an ...

  8. mysql集群 MySQL Cluster

    <?php /* 郑重说明2015年6月11日16:28:14,目前为止MySQL Cluster 社区版不支持INNODB,商业版支持,但是授权价格20W左右,so看此文档之前,考虑下钱 My ...

  9. python - socket - server

    网络上关于socket的介绍文章数不胜数.自己记录下学习的点点滴滴.以供将来复习学习使用. socket中文的翻译是套接字,总感觉词不达意.简单的理解就是ip+port形成的一个管理单元.也是程序中应 ...

  10. P1311 选择客栈

    开始写了一个O(n3)的算法,只得了60,后来思考(找题解),得到了一个O(nk)的算法 其实就是一种预处理的思想,对于每一个客栈而言,只要我们预处理出他前面可以匹配的客栈数量,就可以了. 所以我们记 ...