2016 Multi-University Training Contest 4 1012 Bubble Sort (线段树)

Bubble Sort

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

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

Recommend
wange2014 | We have carefully selected several similar problems for you: 5775 5774 5773 5772 5771

多校好难都不会做。只能做这种水题。。。
用树状数组敲起来更快更简洁。
题意:给你一串数,对数列进行冒泡排序。问你每一个数字在冒泡排序过程中所存在的该数字最左与最右位置的差。
对于最左位置min(初始位置,最终位置),如果初始位置比他的最终位置小那么肯定在冒泡排序过程中他会一直上浮,也就是向右移动,不可能向左,同时到达最终位置后也不会再向右移动了。
对于最右位置的确定是一个动态的过程,假设要确定的这个数字是n,我们逆向扫描数列,将各个数字依次加入,用线段树维护插入的数字,当扫描到我们需要确定的数字的时候,在线段树上查询1,n区间中已经被加入的数字的个数k,说明n的右边有k个数字(这k个都是比n小的数)已经被移动到了n的左边,那么n现在的位置就在初始位置+k的位置,max(移动后的位置,最终位置)就是最右位置,这是因为在排序过程中,一个数可能会被移动到比自己最终位置更右的位置,而最左位置确定时就没有这个问题,不会被移动到比最终位置更左的位置。

#include "cstring"
#include "cstdio"
#include "string.h"
#include "iostream"
using namespace std;
int num[100005];
int ans[100005];
typedef struct node
{
    int left,right,sum;
}node;
node tree[100005*4];

void build(int lf,int rt,int root)
{
    tree[root].left=lf;
    tree[root].right=rt;
    tree[root].sum=0;
    if(lf==rt)
    {
        return;
    }
    int mid=(lf+rt)/2;
    build(lf,mid,root*2);
    build(mid+1,rt,root*2+1);

}
int update(int pos,int root)
{
    int mid=(tree[root].left+tree[root].right)/2;
    if(tree[root].left==pos&&tree[root].right==pos)
    {
        tree[root].sum=1;
        return tree[root].sum;
    }
    if(pos>mid)
        update(pos,root*2+1);
    else
        update(pos,root*2);
    tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
    return tree[root].sum;
}

int query(int left,int right,int root)
{
    if(tree[root].left==left&&tree[root].right==right)
    {
        return tree[root].sum;
    }
    int mid=(tree[root].left+tree[root].right)/2;
    if(left>mid)
    {
        return query(left,right,root*2+1);
    }
    else if(mid>=right)
        return query(left,right,root*2);
    else
    {
        int a=query(left,mid,root*2);
        int b=query(mid+1,right,root*2+1);
        return a+b;
    }
}


int main()
{
    int cas;
    scanf("%d",&cas);
    int cnt=1;
    while(cas--)
    {
        memset(num,0,sizeof(num));
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
        }
        build(1,n,1);
        for(int i=n;i>=1;i--)
        {
            int k=query(1,num[i],1);
            ans[num[i]]=k+i-min(i,num[i]);
            update(num[i],1);
        }
        printf("Case #%d:",cnt++);
        for(int i=1;i<=n;i++)
            printf(" %d",ans[i]);
        printf("\n");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值