Bubble Sort
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 472 Accepted Submission(s): 291
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++.
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.
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.
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 0HintIn 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(没排序之前的位置,排序之后的位置)-max(排序之后的位置,没排序之前的位置+没排序之前后边比它小的数的个数(也就是排序过程中需要往后移动的步数)).重点找比它小的数的个数,用线段树找#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; int a[100010]; int b[100010]; int c[100010]; int d[100010]; int f[100010]; int v[100010]; struct st { int left,right,sum; } w[100010*4]; void build(int l,int r,int tr)//建树 { if(l==r) { w[tr].right=w[tr].left=l,w[tr].sum=0; return ; } int mid=(l+r)>>1; w[tr].left=l,w[tr].right=r,w[tr].sum=0; build(l,mid,tr<<1); build(mid+1,r,tr<<1|1); } int query(int l,int r,int tr,int num)//查树 { if(w[tr].right==w[tr].left) return w[tr].sum; int mid=(l+r)>>1; if(mid<num) return w[tr<<1].sum+query(mid+1,r,tr<<1|1,num); else return query(l,mid,tr<<1,num); } void update(int l,int r,int tr,int num)//更新树 { int mid=(l+r)>>1; w[tr].sum+=1; if(w[tr].left==w[tr].right&&w[tr].right==num) { return ; } else if(num>mid) update(mid+1,r,tr<<1|1,num); else update(l,mid,tr<<1,num); } int main() { int nn,ss=0; scanf("%d",&nn); while(nn--) { int v[100010]={0}; int n,i; scanf("%d",&n); for(i=1; i<=n; i++) scanf("%d",&a[i]),b[a[i]]=i,f[i]=a[i]; build(1,n,1); for(i=n; i>=1; i--) c[a[i]]=query(1,n,1,a[i]),update(1,n,1,a[i]); sort(a+1,a+n+1); for(int i=1;i<=n;i++) d[a[i]]=i; printf("Case #%d:",++ss); int p=0; for(i=1; i<=n; i++) { int h; h=min(b[a[i]],d[a[i]])-max(d[a[i]],b[a[i]]+c[a[i]]); if(h<0) h=-h; v[p++]=h; } for(int i=0;i<p;i++) { printf(" %d",v[i]); } printf("\n"); } }