POJ 2182 Lost Cows 【树状数组+二分】

题目链接:http://poj.org/problem?id=2182

Lost Cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12736   Accepted: 8168

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

题意概括:

有 N 头牛跑散了位置,现在我们可以依次知道他们前面有几头原来编号比他们原来编号小的牛,求他们原来的编号是多少。

解题思路:

①可以直接逆推暴力,因为草稿模拟一下可以知道,我们可以求最后一头牛他的编号是多少(即 F[ N ] + 1,F[ N ] 为他前面有多少头序号比他小的牛,并且他后面没有牛了,所以可以从最小的1开始逆推)。推出了最后一个,接下来可以推出倒数第二个,这里需要用到一个 visi [ x ] 来标记 编号X 是否已经被用;已经被用的编号可以丢掉不考虑了。推倒数第二个的推法跟推倒数第一个的一样,只不过有些比它小的编号已经被用掉了,只需要考虑那些还没用的编号,从这些编号里的最小编号开始往前逆推。所以双重循环可以搞定全部的牛牛了。

②树状数组+二分

树状数组的 SUM( X ) 用于记录 编号X 后面满足小于等于 X 的已经用掉了的编号的个数;F[ i ] 就是题目给出的 第 i 个牛 前面比 第i 个牛的编号小的编号的个数; 我们需要二分的就是 X,判断 X是不是当前第 i 头牛的编号。

如果 (X-1)-SUM( X - 1) == F[ i ] (即 编号X 前面剩下的小于 X 的编号的数量恰好等于 第 i 头牛编号的条件, 则 X 就是 第 i 头牛的编号啦)

如果(X-1)-SUM( X - 1)  > F[ i ]   (说明编号偏大咯)

扫描二维码关注公众号,回复: 2728704 查看本文章

如果(X-1)-SUM( X - 1) < F[ i ]      (说明编号偏小咯)

AC code:

 1 ///树状数组+二分
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <map>
 8 #define ll long long int
 9 #define INf 0x3f3f3f3f
10 using namespace std;
11 
12 const int MAXN = 8e3+10;
13 int f[MAXN];
14 int b[MAXN];
15 int num[MAXN];
16 int N;
17 int lowbit(int x)
18 {
19     return x&(-x);
20 }
21 void add(int x, int value)
22 {
23     for(int i = x; i <= N; i+=lowbit(i))
24         b[i]+=value;
25 }
26 int sum(int x)                       ///后面比x小的数的个数
27 {
28     int res = 0;
29     for(int i = x; i > 0; i-=lowbit(i))
30         res+=b[i];
31     return res;
32 }
33 int main()
34 {
35     scanf("%d", &N);
36     num[1] = 0;
37     for(int i = 2; i <= N; i++)  ///前面比编号为i的数小的数的个数
38         scanf("%d", &f[i]);
39 
40     num[N] = f[N]+1;
41     add(num[N], 1);
42     for(int i = N-1; i > 0; i--)
43     {
44         int l = 1, r = N;
45         while(r > l)
46         {
47             int mid = (l+r)>>1;
48             if(mid-1-sum(mid) >= f[i])
49             {
50                 r = mid;
51             }
52             else
53             {
54                 l = mid+1;
55             }
56         }
57         num[i] = l;
58         add(num[i], 1);
59     }
60     for(int i = 1; i <= N; i++)
61         printf("%d\n", num[i]);
62     return 0;
63 
64 }
View Code

猜你喜欢

转载自www.cnblogs.com/ymzjj/p/9465877.html
今日推荐