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.

【题意】求出每个数能到达的最右和最左的坐标之差

【思路】从最后往前跟新到数组中,并且同时求出有多少个数小于当前数,则他所能到的右坐标是当前位置加上右边小于他的数的个数

左坐标是他min(当前位置,a[i])

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
const int N=100000+10;
int a[N],sum[N*4],pos[N],res[N];
int n;
int lowbit(int p)
{
    return p&(-p);
}
void update(int p)
{
    while(p<=n)
    {
        sum[p]+=1;
        p+=lowbit(p);
    }
}
int get_sum(int p)
{
    int res=0;
    while(p>0)
    {
        res+=sum[p];
        p-=lowbit(p);
    }
    return res;
}
int main()
{
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        memset(sum,0,sizeof(sum));
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            pos[a[i]]=i;
        }
        for(int i=n; i>0; i--)
    //倒序,从当前位置右边找出有多少小于他的数,记录在res中,他的最大右坐标是res+pos
        {
            update(a[i]);
            res[a[i]]=get_sum(a[i]-1);
        }

        printf("Case #%d: ", cas++);
        for(int i=1; i<=n; i++)
        {
            int minx=min(i,pos[i]);
            if(i!=n) printf("%d ",abs(pos[i]+res[i]-minx));
            else printf("%d\n",abs(pos[i]+res[i]-minx));

        }
    }
    return 0;
}
时间: 2024-12-26 07:20:53

Bubble Sort_树状数组的相关文章

hdu 5775 Bubble Sort (树状数组)

Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 591    Accepted Submission(s): 359 Problem Description P is a permutation of the integers from 1 to N(index starting from 1).Here is t

hdu 5775 Bubble Sort 树状数组+冒泡排序

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5775 [题意]按照题目的冒泡排序求每个数在排序过程中到达最右边位置与最左边位置之差. [解题思路]考虑i位置上的数字a[i]在冒泡排序过程的变化情况.a[i]会被其后面比a[i]小的数字各交换一次,之后a[i]就会只向前移动.数组从右向左扫,树状数组维护一下得到每个值右边有多少个比其小的值,加上原位置得到最右位置,最左位置为初始位置和最终位置的最小值. [时间复杂度]O(n lg n)O(n\ lg

HDU 5775 L - Bubble Sort 树状数组

给定一段冒泡排序的代码,要求输出每个数字能到达的最右边的位置和最左边的位置的差 因为那段冒泡排序的代码是每次选取一个最小的数,放在左边的,所以,每个数最多能到达右边的位置应该是起始位置i+右边有多少个数比它大. 它能到达的最左的位置,可以这样考虑 1.它本来应该是排去起始位置的左边的,就是它本来是一个小的数字,起始位置在末尾这种情况的话.最左边的值就是a[i] 2.它是去右边的,那么最左边的值就是起始位置, 两种取max就去可以了 #include <cstdio> #include <

HDU 5775:Bubble Sort(树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=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

hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 636    Accepted Submission(s): 378 Problem Description P is a permutation of the

HDU 5775 Bubble Sort(树状数组)

题目链接:HDU 5775 题面: Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 709    Accepted Submission(s): 418 Problem Description P is a permutation of the integers from 1 to N(index startin

HDU 5775 树状数组

Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 853    Accepted Submission(s): 504 Problem Description P is a permutation of the integers from 1 to N(index starting from 1).Here is t

HDU5122 K.Bro Sorting 【树状数组】

K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 10    Accepted Submission(s): 9 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include