zoukankan      html  css  js  c++  java
  • Bubble Sort (5775)

      Bubble Sort

    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的排列,然后按照冒泡排序的移动方式,问每个i 能移动到的最左位置和最右位置的差是多少

       分析:在冒泡排序过程的变化情况。c会被其后面比c小的数字各交换一次,之后c就会只向前移动。

    数组从右向左扫,树状数组维护一下得到每个值左边有多少个比其小的值通过转换得到每个值右边有多

    少个比其小的值,加上原位置得到最右位置,最左位置为初始位置和最终位置的最小值。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    using namespace std;
    const int maxn = 100005;
    int n;
    int a[maxn], b[maxn],c[maxn],C[maxn];
    void add(int x){
        while(x<=n) C[x]++,x+=(x&-x);
            //cout<<"x: "<<x<<"  C[x]: "<<C[x]<<endl,
    }
    
    int sum(int x){
        int ans=0;
        while(x>0)
            ans+=C[x],x-=(x&-x);
            //cout<<"ans: "<<ans<<endl;
        return ans;
    }
    
    
    
    int main()
    {
        int t;
        int kase = 0;
        scanf("%d", &t);
        while(t--)
        {
         memset(c, 0, sizeof(c));
            memset(b, 0, sizeof(b));
            int i;
            scanf("%d", &n);
            for(i=1; i<=n; i++ )
            {
                scanf("%d", &a[i]);
                b[a[i]] = i;
               }
            memset(C,0,sizeof(C));
            for(int i=1;i<=n;i++){
                c[i]=sum(a[i]-1);
                add(a[i]);
               // cout<<c[i]<<".."<<endl;
            }
    
            sort(a+1,a+n+1);
            printf("Case #%d:", ++kase);
            for(i = 1; i <= n; i++ )
            {
                int x=i-c[b[i]]-1;
                  printf(" %d", max(abs(b[i]+x-b[i]),abs(b[i]+x-i)));
    
            }
            printf("
    ");
        }
    
        return 0;
    }
    //自己的绕了点弯路
    View Code
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn=101000;
    int C[maxn],a[maxn],id[maxn],l[maxn],r[maxn],n;
    
    void add(int x){
        while(x<=n)
    
            {
                 C[x]++,x+=(x&-x);
                 }
    }
    
    int sum(int x){
        int ans=0;
        while(x>0)
            ans+=C[x],x-=(x&-x);
        return ans;
    }
    
    int main(){
        int _;
        scanf("%d",&_);
        for(int case1=1;case1<=_;case1++){
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
                scanf("%d",&a[i]),l[a[i]]=i;
            memset(C,0,sizeof(C));
            for(int i=n;i>=1;i--){
                r[a[i]]=i+sum(a[i]-1);
                add(a[i]);
            }
            printf("Case #%d: ",case1);
            for(int i=1;i<=n;i++){
                if(i!=n)
                    printf("%d ",r[i]-min(l[i],i));
                else
                    printf("%d
    ",r[i]-min(l[i],i));
            }
        }
        return 0;
    }
    //网上的
    View Code
  • 相关阅读:
    tp5 宝塔open_basedir restriction in effect 错误; IIS open_basedir restriction in effect
    如何封装一个自己的win7系统并安装到电脑做成双系统
    推荐7个模板代码和其他游戏源码下载的网址
    PHP简单实现异步多文件上传并使用Postman测试提交图片
    PHP公众号开发给用户发微信消息提醒功能
    解决在页面中无法获取qrcode.js生成的base64的图片
    如何使用GUID硬盘分区格式安装新windows系统
    超详细的纯净windows系统重装示例
    Spark之join、leftOuterJoin、rightOuterJoin及fullOuterJoin
    Spark中groupByKey、reduceByKey与sortByKey
  • 原文地址:https://www.cnblogs.com/fenhong/p/5717453.html
Copyright © 2011-2022 走看看