zoukankan      html  css  js  c++  java
  • HDU

    Bubble Sort

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 659    Accepted Submission(s): 393

    Problem Description
    P is a permutation of the integers from 1 to N(index starting from 1).
    Here is the code of Bubble Sort in C++.
    for(int i=1;i<=N;++i)
        for(int j=N,t;j>i;—j)
            if(P[j-1] > P[j])
                t=P[j],P[j]=P[j-1],P[j-1]=t;
    

    After the sort, the array is in increasing order. ?? wants to know the absolute values of difference of rightmost place and leftmost place for every number it reached.
     
    Input
    The first line of the input gives the number of test cases T; T test cases follow.
    Each consists of one line with one integer N, followed by another line with a permutation of the integers from 1 to N, inclusive.

    limits
    T <= 20
    1 <= N <= 100000
    N is larger than 10000 in only one case. 
     
    Output
    For each test case output “Case #x: y1 y2 … yN” (without quotes), where x is the test case number (starting from 1), and yi is the difference of rightmost place and leftmost place of number i.
     
    Sample Input
    2 3 3 1 2 3 1 2 3
     
    Sample Output
    Case #1: 1 1 2 Case #2: 0 0 0
    Hint
    In first case, (3, 1, 2) -> (3, 1, 2) -> (1, 3, 2) -> (1, 2, 3) the leftmost place and rightmost place of 1 is 1 and 2, 2 is 2 and 3, 3 is 1 and 3 In second case, the array has already in increasing order. So the answer of every number is 0.
     
    题意:给一个 1 - N 的排列,它经过冒泡排序(从后往前)恢复成正常的递增序列。问在冒泡的过程中,每个数字最左位置和最右位置的差的绝对值。

    思路:仔细想一下会发现每个数字只会被它后面的比它小的数字影响,且会向右移动相应个数的位置。比如:6 4 3 5 2 1 。4后面比它小的有 三个,因此它的最右边位置就是当前位置 +3,即5。如果该数字本身在标准位置左边,那无须考虑,初始位置就是最左端,如果它在标准位置右边,我们可以知道,它最终肯定要回到标准位置,所以最左边应该为标准位置。


    利用树状数组从后往前维护一下比当前位置小的元素个数,并插入树状数组即可。(每次插入前判断从 1 到 该数字 之间有没有数字,利用sum (i) 可以求得,之后再插入该数字即可,插到对应数字的位置)

    代码:
     1 #include <iostream>
     2 #include <cstring>
     3 using namespace std;
     4 
     5 int a[100005];
     6 int ans[100005];
     7 int c[100005];
     8 
     9 int lowbit(int x){
    10     return x & (-x);
    11 }
    12 void add(int id,int p){
    13     while(id <= 100000){
    14         c[id] += p;
    15         id += lowbit(id);
    16     }
    17 }
    18 int sum(int id){
    19     int sum = 0;
    20     while(id >= 1){
    21         sum += c[id];
    22         id -= lowbit(id);
    23     }
    24     return sum;
    25 }
    26 int main()
    27 {
    28     ios_base::sync_with_stdio(0);
    29     cin.tie(0);
    30 
    31     int T;
    32     cin>>T;
    33     int cnt = 0;
    34     while(T--){
    35         int n;
    36         cin>>n;
    37         memset(a,0,sizeof(a));
    38         memset(ans,0,sizeof(ans));
    39         memset(c,0,sizeof(c));
    40         for(int i = 1;i <= n;i ++)  cin>>a[i];
    41 
    42         for(int i = n;i >= 1;i --){
    43             int res = sum(a[i]);        //判断比该数字小的区间内有多少数字
    44             ans[a[i]] = res;
    45             add(a[i],1);        //插入该数字到标准位置
    46         }
    47         for(int i = 1;i <= n;i ++)
    48             if(i >= a[i])
    49                 ans[a[i]] += (i-a[i]);      //初始位置在标准位置右边,最左端为标准位置,更新|left - right|
    50         cout<<"Case #"<<++cnt<<": ";
    51         for(int i = 1;i <= n;i ++){
    52             if(i != 1)  cout<<" ";
    53             cout<<ans[i];
    54         }
    55         cout<<endl;
    56     }
    57     return 0;
    58 }
    View Code
  • 相关阅读:
    Xml命名空间添加前缀的意义
    解决vs Installer无法下载更新
    mongodb模糊查询
    mongodb排序
    mongodb如何修改_id
    向Mongodb中插入数组元素
    Mongodb 常见操作符
    452.用最少数量的箭引爆气球
    450.删除二叉搜索树中的节点-medium
    45.跳跃游戏 II
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6351927.html
Copyright © 2011-2022 走看看