本文主要是介绍HDU 5775 (Bubble Sort 逆序数),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接
意思是告诉你一个冒泡排序,在整个排序的过程中问你每个数出现最左与最右的位置差
其实就是求每个数后面有几个比他小的,因为后面小的排序会移到他前面,用树状数组写一下就可以了
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<bits/stdc++.h>
#include<map>
#include<string>
#include<cstdlib>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define LL long long
#define pb push_back
#define gcd __gcd#define For(i,j,k) for(int i=(j);i<k;i++)
#define lowbit(i) (i&(-i))
#define _(x) printf("%d\n",x)const int maxn = 1e6+200;
const int inf = 1 << 28;struct BIT{LL c[maxn],sz;void init(int s){sz=s;for(int i=0;i<=sz;c[i]=0,i++);}void updata(int x,int val){while(x<=sz){c[x]+=val;x+=lowbit(x);}}LL sum(int x){LL sum=0;while(x>0){sum+=c[x];x-=lowbit(x);}return sum;}
}bit;
int a[maxn];
int ans[maxn];
int main(){int T;scanf("%d",&T);int cas = 1;while(T--){int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);}printf("Case #%d:",cas++);bit.init(n);for(int i=n;i>=1;i--){bit.updata(a[i],1);int x = bit.sum(a[i])-1;int right = max(a[i],i+x);int left = min(a[i],i);int y=abs(left-right);ans[a[i]]=y;}for(int i=1;i<=n;i++)printf(" %d",ans[i]);puts("");}return 0;
}
这篇关于HDU 5775 (Bubble Sort 逆序数)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!