丢失的牛?_Lost Cows_POJ2182_线段树
来源:互联网 发布:网络电影疯狂小镇 编辑:程序博客网 时间:2024/06/11 08:07
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
题目大意:
有n头牛,编号为1~n,它们并没有按照编号的顺序排好队列。现在只知道每一个牛前面有多少只牛的编号比它小。求所有牛的编号。
太水,懒得讲
源代码/pas:
#include <stdio.h>#include <stack>using namespace std;struct tree{int l,r,c;}t[24001];stack<int>ans;int s[8001];void build(int f,int x,int y){t[f]=(tree){x,y,y-x+1};int mid=(x+y)>>1;if (x==y)return;build(f+f,x,mid);build(f+f+1,mid+1,y);}int count(int f,int x){--t[f].c;if (t[f].l==t[f].r)return t[f].r;if (t[f+f].c<x)return count(f+f+1,x-t[f+f].c);elsereturn count(f+f,x);}int main(){int n;scanf("%d",&n);for (int i=2;i<=n;i++)scanf("%d",&s[i]);build(1,1,n);for (int i=n;i>=1;i--)ans.push(count(1,s[i]+1));while (!ans.empty()){printf("%d\n",ans.top());ans.pop();}return 0;}
typetree=record l,r,c:longint;end;var t:array[0..24000]of tree; a:array[0..8001]of longint; n:longint;procedure build(f,x,y:longint);var mid:longint;begin t[f].l:=x; t[f].r:=y; t[f].c:=y-x+1; mid:=(x+y)div 2; if x=y then exit; build(f*2,x,mid); build(f*2+1,mid+1,y);end;function count(f,x:longint):longint;begin dec(t[f].c); if t[f].l=t[f].r then exit(t[f].l) else if t[f*2].c<x then exit(count(f*2+1,x-t[f*2].c)) else exit(count(f*2,x));end;procedure main;var i:longint; s:array[0..8001]of longint;begin for i:=n downto 1 do s[i]:=count(1,a[i]+1); for i:=1 to n do writeln(s[i]);end;procedure init;var i:longint;begin readln(n); for i:=2 to n do read(a[i]); build(1,1,n);end;begin init; main;end.
- 丢失的牛?_Lost Cows_POJ2182_线段树
- 丢失的牛
- USACO 丢失的牛
- 牧人和丢失的牛
- 大牛整理的线段树集锦
- 线段树的知识
- 线段树的建立
- 线段树的题目
- 线段树的建立
- 线段树的应用
- 线段树的建树
- 我的线段树
- 线段树的题目
- 线段树的总结
- 线段树的实现
- 线段树的修改
- 线段树的构造
- 线段树的查询
- Java多线程面试问题
- SICP 1.2.6 素数检测
- Android Studio安装插件 重启后保存不上的问题
- 二叉树遍历算法总结
- 理解I2C接口
- 丢失的牛?_Lost Cows_POJ2182_线段树
- C++ Primer复制控制 13.3 析构函数
- 矩阵理解与线性代数直觉性丧失
- Android解析聚合数据之天气预报
- sqlite3数据库c语言常用接口应用实例
- HomeWork——Binary Search Tree
- Linux iptables配置实践
- virtualenv 指定 python 解释器的版本
- 收获很大的现场面试经历