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个区间,对于任意一个区间,求出能完全包含它并且长度比它长的区间的个数. 题目分析:将一个区间视为二位坐标系中的一个点,左端点视作横坐标,右端点视作纵坐标.则题目变成了求每个点的左上方.正 ...
随机推荐
- js的关联数组
我自己感觉js是不支持关联数组的,只有索引数组.想要实现js关联数组的效果,就使用js的对象,使用键值对.如果对数组动态处理用push函数,取值用pop函数.此外,对数组操作有几个比较好的函数,joi ...
- 用Webshell直接杀入内网
有时候要代理进内网,但是服务器没有外网端口,也没法执行命令反弹lcx什么的,也没有提权什么什么的,只有一个webshell,这个时候就比较蛋疼了. 鄙人不才,写了一个比较山寨的php+nginx反向代 ...
- JavaScript 几种简单的table切换
方法一:for循环+if判断当前点击与自定义数组是否匹配 <!DOCTYPE html> <html lang="en"> <head> < ...
- Servlet获取URL地址
这里来说说用Servlet获取URL地址.在HttpServletRequest类里,有以下六个取URL的函数: getContextPath 取得项目名 getServletPath 取得Servl ...
- IOS第二天多线程-02一次性代码
********** #import "HMViewController.h" #import "HMImageDownloader.h" @interface ...
- IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式)
***控制层 #import "ViewController.h" #import "CZKeyboardToolbar.h" @interface ViewC ...
- 【iCore3 双核心板_ uC/OS-III】例程九:任务信号量
实验指导书及代码包下载: http://pan.baidu.com/s/1c1W29uK iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- velocity.js用法整理1
velocity.js 此框架相对于JQ的运动算法, 有很大的优势. 例如,A和B两个元素,position:absolute; top:0; 现在让A元素用JQ的animate,B用velocit ...
- Apache Spark技术实战之4 -- 利用Spark将json文件导入Cassandra
欢迎转载,转载请注明出处. 概要 本文简要介绍如何使用spark-cassandra-connector将json文件导入到cassandra数据库,这是一个使用spark的综合性示例. 前提条件 假 ...
- Run P4 without P4factory - A Simple Example In Tutorials. -2 附 simple_router源码
/* Copyright 2013-present Barefoot Networks, Inc. Licensed under the Apache License, Version 2.0 (th ...