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 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.

Author

FZU

Source

2016 Multi-University Training Contest 4

题目大意:给出一个序列,求对于每一个数字在冒泡排序过程中最大最小下标的差值。

解题思路:

我们从小到大考虑每一个数组,计算出他左边比他小的个数,再用n减去就是右边比他小的数量了,采用树状数组数组的方式来进行运算。

详见代码。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;

#define N 100000+10
int c[N],a[N],ans[N];

int lowbit(int k)
{
    return k&((k)xor(k-1));
}

void add(int num,int k)
{
    while (k<=N)
    {
        c[k]+=num;
        k+=lowbit(k);
    }
}

int sum(int k)
{
    int s=0;
    while (k)
    {
        s+=c[k];
        k-=lowbit(k);
    }
    return s;
}

int main()
{
    int t,s,Case=1;
    scanf("%d",&t);
    while (t--)
    {
        int n,k,Max,Min;
        scanf("%d",&n);
        memset(c,0,sizeof(c));
        for (int i=1; i<=n; i++)
        {
            k=0;
            scanf("%d",&a[i]);
            add(1,a[i]);//在a[i]的位置加1
            s=a[i]-sum(a[i]);//a[i]后面有多少个比当前数值小的
            Max=max(a[i],i+s);
            Min=min(a[i],i);
            ans[a[i]]=Max-Min;
        }
        printf ("Case #%d:",Case++);
        for (int i=1; i<=n; i++)
            printf (" %d",ans[i]);
        printf ("\n");
    }
    return 0;
}
时间: 2024-12-28 08:37:22

hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)的相关文章

hdu 5792 World is Exploding(2016 Multi-University Training Contest 5——树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 643    Accepted Submission(s): 306 Problem Description Given a sequence A

HDU 5775 Bubble Sort(冒泡排序)

HDU 5775 Bubble Sort(冒泡排序) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   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++.

HDU 5618:Jam&#39;s problem again(CDQ分治+树状数组处理三维偏序)

http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意:-- 思路:和NEUOJ那题一样的.重新写了遍理解了一下,算作处理三维偏序的模板了. 1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #include <cstring> 5 using namespace std; 6 #define INF 0x3f3f3f3f 7 #d

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 (树状数组)

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 5249区间第k大(学习了下树状数组的搞法)

KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 205    Accepted Submission(s): 70 Problem Description 你工作以后, KPI 就是你的全部了. 我开发了一个服务,取得了很大的知名度.数十亿的请求被推到一个大管道后同时服务从管头拉取请求.让我们来定义每个请求都有一个重要值.我的K

HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

题目大意:给一个N个点M条边的无向图,有Q个询问:1.删掉a.b之间所存在的边:2.询问有多少条边,单独删掉之后a与b不再连通. 思路:脑洞大开. 对于询问,首先想到的就是a与b之间有多少桥(割边),然后想到双连通分量,然而删边是个坑爹的问题,于是我们离线倒着来,把删边变成加边. 双连通分量这种东西呢,其实缩点连起来之后,就是一棵树辣. 然后询问两个点的时候,设根到点x的距离为dep[x],a.b的最近公共祖先为lca(a, b),那么询问query(a, b) = dep[a] + dep[b

HDU - 5775 Bubble Sort

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 o