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 ‘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到n的排列,但是,一开始,这个排列只告诉了你从第二个数字开始的比这个数字小的数的个数。

解题思路:

  刚开始想了好久,还是自己太弱,对于数学方面的问题还应该多做题,CQ省队爷告诉我,应该先由一般的规律推出来如果你要确定某个数字p,那么我们就要知道p的前面有多少个数字比它小,记为m,p的后面有多少个数字比他小,记为n,那么p的位置就该在这个数列中的第m+n+1处,我们就二分这个p,拿每次得到的mid来和m+n+1来比较大小,如果m+n+1>mid就说明mid取小了,这个时候,我们就把left = mid+1;如果m+n+1<mid,那就说明mid取大了,这个时候我们就要把mid = right.

  关于一个数字后面比他小的数字的个数怎么求解的问题,其实可以用树状数组来搞定。

  关于一个数字前面有多少个数字比他小的问题,我们再输入的过程中已经知道了。

代码:

 1 # include<cstdio>
 2 # include<iostream>
 3 # include<fstream>
 4 # include<algorithm>
 5 # include<functional>
 6 # include<cstring>
 7 # include<string>
 8 # include<cstdlib>
 9 # include<iomanip>
10 # include<numeric>
11 # include<cctype>
12 # include<cmath>
13 # include<ctime>
14 # include<queue>
15 # include<stack>
16 # include<list>
17 # include<set>
18 # include<map>
19
20 using namespace std;
21
22 const double PI=4.0*atan(1.0);
23
24 typedef long long LL;
25 typedef unsigned long long ULL;
26
27 # define inf 999999999
28 # define MAX 8000+4
29
30 int a[MAX];
31 int ans[MAX];
32 int tree[MAX];
33
34 int n;
35
36 int read ( int pos )
37 {
38     int ans = 0;
39     while ( pos > 0 )
40     {
41         ans+=tree[pos];
42         pos-=pos&(-pos);
43     }
44     return ans;
45 }
46
47 void update ( int pos,int val )
48 {
49     while ( pos <= n )
50     {
51         tree[pos]+=val;
52         pos+=pos&(-pos);
53     }
54 }
55
56 int my_search ( int k )
57 {
58     int left = 1, right = n;
59     while ( left < right )
60     {
61         int mid = ( left+right )>>1;
62         int num = read (mid);
63         if ( mid - 1 < num+k )
64         {
65             left = mid+1;
66         }
67         else
68         {
69             right = mid;
70         }
71     }
72     return left;
73 }
74
75 int main(void)
76 {
77     while ( scanf("%d",&n)!=EOF )
78     {
79         memset(tree,0,sizeof(tree));
80         memset(ans,0,sizeof(ans));
81         a[1] = 0;
82         for ( int i = 2;i <= n;i++ )
83             scanf("%d",&a[i]);
84         for ( int i = n;i >= 1;i-- )
85         {
86             int k = my_search(a[i]);
87             update(k,1);
88             ans[i] = k;
89         }
90
91         for ( int i = 1;i <= n;i++ )
92             printf("%d\n",ans[i]);
93     }
94
95
96
97     return 0;
98 }
时间: 2024-12-27 23:02:06

POJ 2182 Lost Cows (树状数组)的相关文章

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

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

POJ 2481 Cows(树状数组)

Description Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good. Farmer John has N cows (we number the cows from 1 to N). Ea

hdu 1541/poj 2352:Stars(树状数组,经典题)

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4052    Accepted Submission(s): 1592 Problem Description Astronomers often examine star maps where stars are represented by points on a plan

POJ 2892 Tunnel Warfare (树状数组+二分)

题目大意: 三个操作 D pos  将pos位置摧毁,让它和周围不相连. Q pos 问和pos 相连的有多少个村庄. R 修复最近摧毁的村庄. 思路分析: 树状数组记录这个区间有多少个1. 如果  [s-e] 有e-s+1个1 的话.那么这个区间是相连的. 这样的话,我们就可以用二分的办法求出与某个位置最大相连的数量. 还有这里二分 while(l<=r) { if(满足) { ans=mid; l=mid+1; } else r=mid-1; } #include <cstdio>

poj 2155 二维树状数组

http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17721   Accepted: 6653 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. I

poj 2309 BST 使用树状数组的lowbit

如果领悟了树状数组中的lowbit,这道题就是极其简单的,最底层都是奇数,用lowbit(x)寻找x的父亲,然后x的父亲-1就是最大数 至于lowbit是如何计算的嘛,寻找x的父亲,其实就是x+2^x的二进制末尾0的个数. #include<iostream> #include<stdio.h> using namespace std; typedef long long ll; ll lowbit(int x){ return x&(-x); } int main(){

POJ2481 Cows 树状数组的简单应用

题意给了你N头牛,每头牛的强壮值是一个区间[s,e],如果第 i 头牛比第 j 头牛强壮那么必须满足 Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj: 为了满足这三个条件我们进行排序,先以e降序排为先决条件,若e相等则让s升序排列,如此即可满足三个条件,这道题目任意两头牛的强壮值区间有可能完全一样,这样就不需要重新用树状数组求一次和了,直接赋值即可,这样可以省很多时间,因为已经排序处理了,所以即便区间相等的  肯定就是相邻的,所以直接扫一遍即可,若

poj 1195 二维树状数组 及二维树状数组模板

http://poj.org/problem?id=1195 求矩阵和的时候,下标弄错WA了一次... 求矩形(x1,y1) (x2,y2)的sum |sum=sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1) 二维树状数组讲解:http://blog.csdn.net/u011026968/article/details/38532117 二维树状数组模板: /*========================================

poj 2481 Cows 树状数组解法,详细解析。

Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13445   Accepted: 4448 Description Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in hi