HDU 5775 Bubble Sort

对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置。

初始位置和终点位置容易计算。最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小。

三个位置中的min即为leftpos,max即为rightpos

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int maxn=+;
int n,a[maxn],c[maxn],p[maxn],L[maxn],R[maxn]; int lowbit(int x) { return x&(-x); }
void update(int x) { while(x<=n) c[x]+=,x+=lowbit(x); }
int get(int x) {int res=; while(x) res=res+c[x],x-=lowbit(x); return res;} int main()
{
int T; scanf("%d",&T); int cas=;
while(T--)
{
scanf("%d",&n); memset(c,,sizeof c);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),L[a[i]]=min(i,a[i]),R[a[i]]=max(i,a[i]);
for(int i=;i<=n;i++)
{
int x=get(a[i]-); x=a[i]--x; update(a[i]);
L[a[i]]=min(L[a[i]],i+x), R[a[i]]=max(R[a[i]],i+x);
} printf("Case #%d:",cas++);
for(int i=;i<=n;i++) printf(" %d",abs(L[i]-R[i]));
printf("\n");
}
return ;
}
上一篇:关于DCMTK3.6.1源代码编译的总结


下一篇:php的扩展配置