• Lost Cows(BIT poj2182)


    Lost Cows
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10609   Accepted: 6797

    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
    题意:有一个序列a:1,2,…,N(2 <= N <= 8,000). 现该序列为乱序,已知第i个数前面的有a[i]个小于它的数。求出该序列的排列方式。  
    暴力求解:O(n*n)
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <set>
     4 using namespace std;
     5 int num[10000],a[10000];
     6 int main()
     7 {
     8     int n;
     9     int i,j;
    10     freopen("in.txt","r",stdin);
    11     while(scanf("%d",&n)!=EOF)
    12     {
    13         int k=0;
    14         set<int> s;
    15         set<int>::iterator p;
    16         for(i=2;i<=n;i++)
    17             scanf("%d",&a[i]);
    18         for(i=0;i<n;i++)
    19             s.insert(i+1);
    20         for(i=n;i>=2;i--)
    21         {
    22             p=s.begin();
    23             while(a[i]--)    p++;
    24             num[i]=*p;                
    25             s.erase(*p);
    26         }
    27         num[i]=*s.begin();
    28         for(i=1;i<=n;i++)
    29             printf("%d
    ",num[i]);
    30     }
    31 }
    View Code

     树状数组 O(nlogn)

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 int n;
     6 int a[10000],bit[10000];
     7 int num[10000];
     8 int lowbit(int i)
     9 {
    10     return i&-i;
    11 }
    12 void add(int i,int s)
    13 {
    14     while(i<=n)
    15     {
    16         bit[i]+=s;
    17         i=i+lowbit(i);
    18     }
    19 }
    20 int sum(int i)
    21 {
    22     int s=0;
    23     while(i>0)
    24     {
    25         s+=bit[i];
    26         i=i-lowbit(i);
    27     }
    28     return s;
    29 }
    30 int main()
    31 {
    32     int i,j;
    33     freopen("in.txt","r",stdin);
    34     while(scanf("%d",&n)!=EOF)
    35     {
    36         int s=0;
    37         memset(bit,0,sizeof(bit));
    38         for(i=2;i<=n;i++)
    39             scanf("%d",&a[i]);
    40         for(i=1;i<=n;i++)
    41             add(i,1);
    42         for(i=n;i>=2;i--)
    43         {
    44             int l=1,r=n,mid;
    45             int tem=a[i]+1,p;
    46             while(l<=r)
    47             {
    48                 int mid=(r+l)/2;
    49                 if(sum(mid)>=tem)
    50                 {
    51                     p=mid;
    52                     r=mid-1;
    53                 }
    54                 else
    55                 {
    56                     l=mid+1;
    57                 }
    58             }
    59             s+=p;
    60             num[i]=p;
    61             add(p,-1);
    62         }
    63         num[1]=(n+1)*n/2-s;
    64         for(i=1;i<=n;i++)
    65             printf("%d
    ",num[i]);
    66     }
    67 }
    View Code

  • 相关阅读:
    Apache 浏览器访问限制配置
    Apache 防盗链配置
    Apache 静态缓存配置
    Apache 日志管理
    Apache 域名跳转配置
    搭建完全分布式的hadoop[转]
    Laravel Cheat 表 http://cheats.jesse-obrien.ca/#
    spring-data-mongodb必须了解的操作
    Java MongoDB 资料集合
    MongoDB分片技术[转]
  • 原文地址:https://www.cnblogs.com/a1225234/p/5035536.html
Copyright © 2020-2023  润新知