Lost Cows(线段树 POJ2182)
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)的更多相关文章
- [poj2182] Lost Cows (线段树)
线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...
- POJ 2481 Cows (线段树)
Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每仅仅牛有一个值[S,E],假设对于牛i和牛j来说,它们的值满足以下的条件则证明牛i比牛j强壮:Si &l ...
- POJ 2182 Lost Cows (线段树)
题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ...
- hdu 2711&&poj2182 Lost Cows (线段树)
从后往前查第一个为0的奶牛肯定应该排在第一个.每次从后往前找到第一个为0的数,这个数应该插在第j位.查找之后,修改节点的值为极大值,当整棵树的最小值不为0的时候查找结束. 至于这种查找修改的操作,再没 ...
- poj2182(线段树求序列第k小)
题目链接:https://vjudge.net/problem/POJ-2182 题意:有n头牛,从1..n编号,乱序排成一列,给出第2..n个牛其前面有多少比它编号小的个数,记为a[i],求该序列的 ...
- POJ2182题解——线段树
POJ2182题解——线段树 2019-12-20 by juruoOIer 1.线段树简介(来源:百度百科) 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线 ...
- POJ 2182&& POJ 2828:Lost Cows 从后往前 线段树
Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10544 Accepted: 6754 Descri ...
- ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)
这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...
- POJ-2481 Cows (线段树单点更新)
题目大意:给n个区间,对于任意一个区间,求出能完全包含它并且长度比它长的区间的个数. 题目分析:将一个区间视为二位坐标系中的一个点,左端点视作横坐标,右端点视作纵坐标.则题目变成了求每个点的左上方.正 ...
随机推荐
- ubuntu安装配置jdk tomcat mysql ...
安装之前大家一定要检查好各个版本问题 以免造成不兼容. (一)下载所需安装包: jdk-7u76-linux-x64.tar.gz apache-tomcat-7.0.63.tar.gz MySQL- ...
- bootstrap-combined.min.css
/*! * Bootstrap v2.2.2 * * Copyright 2012 Twitter, Inc * Licensed under the Apache License v2.0 * ht ...
- .net Sql语句批量插入数据库数据
#region 使用SqlBulkCopy public static bool ExecuteTransactionScopeInsert(DataTable dt, int batchSize) ...
- dialog弹层的方式
1 增加一个层<div class="dialogLayer"></div>, 要不就利用伪元素 ::before 2 利用box-shadow: 0 0 ...
- ios-改变button四个角的弧度
-(void)createTitleView{ UIView * backview = [[UIView alloc]init]; backview.frame =CGRectMake(87*kHei ...
- Android NDK开发之Jni调用Java对象
https://my.oschina.net/zhiweiofli/blog/114064 通过使用合适的JNI函数,你可以创建Java对象,get.set 静态(static)和 实例(instan ...
- nodejs 80端口监听失败及NODE_PATH不起作用的问题
nodejs做web服务器,打开80时报错:Error: listen EACCES 0.0.0.0:80 80端口监听失败,是因为1024以下的端口需要root权限,需要sudo或su之后执行.但这 ...
- 针对ajax执行后swiper特效无法执行解决方案
ajax执行后重新绑定swiper事件.
- [转][Android][Android Studio] *.jar 与 *.aar 的生成与*.aar导入项目方法
转自:http://blog.csdn.net/qiujuer/article/details/39754517?utm_source=tuicool [Android][Android Studi ...
- JAVA Math类
public class MathTest{ public static void main(String[] args) { /*---------下面是三角运算---------*/ //将 ...