zoukankan      html  css  js  c++  java
  • HDU 5775 Bubble Sort

    Bubble Sort

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


    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.
     
    直接树状数组,记录前面比当前数字大的数字有多少个 树状数组得到x。那么i-x就是这个数字移动过程的最左端。最右端为初始位置和最终位置的最大值max(a[i],i)
     
    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/7/28 12:32:55
    File Name     :p412.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 100010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    int n;
    int a[maxn];
    int c[maxn];
    int lowbit(int i){
        return i&(-i);
    }
    void add(int i,int d){
        while(i<maxn){
            c[i]+=d;
            i+=lowbit(i);
        }
    }
    int sum(int i){
        int ans=0;
        while(i>=1){
            ans+=c[i];
            i-=lowbit(i);
        }
        return ans;
    }
    //int d[maxn];
    int ans[maxn];
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int T,t;
        cin>>T;
        for(int t=1;t<=T;t++){
            scanf("%d",&n);
            cle(c);
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }
            for(int i=1;i<=n;i++){    
                add(a[i],1);
                /*int x=i-sum(a[i]);//前边有多少比a[i]大的数
                d[a[i]]=i-x;
                d[a[i]]为最左边的下标
                max(a[i],i)为最右边的下标
                */
                ans[a[i]]=max(a[i],i)-sum(a[i]);//这里是化简后的式子
            }
            printf("Case #%d: ",t);
            for(int i=1;i<=n;i++){
                printf("%d",ans[i]);
                if(i<n)printf(" ");
                else puts("");
            }
        }
        return 0;
    }
  • 相关阅读:
    HDU 4691
    cin和scanf的速度差别
    一点点webservice的小知识
    Geetest 极验验证 验证图片拼图
    web api post传一个参数时 值永远是null
    关于支付宝支付的
    发布网站后只能在服务器上访问 外网访问不了??
    sidePagination: "server"和responseHandler: responseHandler
    .net 找回密码的第一步 第二步 第三步的进程条
    关于设置一个全局只读变量来实现验证
  • 原文地址:https://www.cnblogs.com/pk28/p/5716095.html
Copyright © 2011-2022 走看看