poj 2182 Lost Cows

Lost Cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9536   Accepted: 6146

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

 1 #include <cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string>
 5 #include<cstring>
 6 #include<stack>
 7 using namespace std;
 8 #define N 8000
 9 struct node{
10     int l,r,num;
11 };
12 node tree[3*N];
13 int ans[N+5];
14 void build(int num,int l,int r){//建线段树
15     tree[num].num=r-l+1;
16     tree[num].l=l;
17     tree[num].r=r;
18     if(l==r){//到根节点
19         //cout<<" "<<l<<endl;
20         return;
21     }
22     int mid=(l+r)>>1;
23     build(num<<1,l,mid);
24     build(num<<1|1,mid+1,r);
25 }
26 int update(int num,int be){//更新
27     tree[num].num--;
28     if(tree[num].l==tree[num].r){
29         return tree[num].l;
30     }
31     if(tree[num<<1].num>=be){
32         return update(num<<1,be);
33     }
34     else{
35         return update(num<<1|1,be-tree[num<<1].num);
36     }
37 }
38 int main(){
39     int n;
40     cin>>n;
41     int i;
42     ans[0]=0;  //第一个前面有0个
43     for(i=1;i<n;i++){
44         scanf("%d",&ans[i]);
45     }
46     build(1,1,n);
47     for(i=n-1;i>=0;i--){
48         ans[i]=update(1,ans[i]+1);
49     }
50     for(i=0;i<n;i++){
51         cout<<ans[i]<<endl;
52     }
53     return 0;
54 }
时间: 2024-10-20 15:31:37

poj 2182 Lost Cows的相关文章

POJ 2182 Lost Cows.(线段树)

~~~~ 数据太水,暴力无压力,不过还是用线段树写了下,表现了楼主对线段树的无限热爱之情. ~~~~ 题目连接:http://poj.org/problem?id=2182 大致题意;牛牛因XXXXXX原因乱序成一排,现已知每头牛前面有多少头牛比它的编号小(第一头牛前面没有当然就不列粗来了),求每头牛的编号. 思路:从后往前扫描,遇到a,则说明它是剩余序列的第a+1头牛. ~~~~ 暴力代码:(157ms) #include<cstdio> #include<algorithm>

poj 2182 Lost Cows (线段树)

 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8838   Accepted: 5657 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 'wate

poj 2182 Lost Cows(线段树经典题)

题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9152   Accepted: 5879 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, th

POJ 2182 Lost Cows(牛排序,线段树)

Language: Default Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9207   Accepted: 5922 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 nei

POJ 2182 Lost Cows 巧妙解法

题目 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11766   Accepted: 7570 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 'wat

POJ 2182 Lost Cows (树状数组)

Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9660   Accepted: 6219 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 'waterin

18.10.15 POJ 2182 Lost Cows(线段树)

描述 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 me

线段树/树状数组 POJ 2182 Lost Cows

题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号:为1,倒数第二头牛的编号为除去最后一头牛的编号后的第a[i-1] + 1编号:为3,其他的类推,所以可以维护之前已经选掉的编号,求第k大的数字,sum[rt] 表示该区间已经被选掉的点的个数.另外树状数组也可以做,只不过用二分优化查找第k大的位置. 收获:逆向思维,求动态第K大 代码(线段树): /

POJ 2182 Lost Cows (树状数组 &amp;&amp; 二分查找)

题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析:如果倒着看这个序列的话, 那序列的最后一个元素就能够确定一个编号.举个例子:如果序列的最后一个元素为0, 那就说明这头牛前面再也没有比它编号更小的牛了, 所以这头牛的编号肯定是最大的, 我们只要给它所在的编号加个标记, 然后继续根据倒数第二个.第三个--来依次确定便可还原整个序列, 这里可以使用树