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 Descriptionp is a permutation of the integers from 1 to N (index starting from 1).
Here are 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 was in increasing order. ?? Wants to know the absolute values of difference of rightmost place and leftmost place for every number it reached.
Inputthe 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, Inc Lusive.
Limits
T <= 20
1 <= N <= 100000
N is larger than 10000 in only one case.
Outputfor 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 Input233 1 231 2 3
Sample outputcase #1:1 1 2Case #2:0 0 0
HintIn first case, (3, 1, 2)--(3, 1, 2)--(1, 3, 2)--(1, 2, 3) the leftmost place and rightmost place of 1 is 1 a nd 2, 2 is 2 and 3, 3 are 1 and 3In second case, the array had already in increasing order. The answer of every number is 0. Test instructions is to give a sequence composed of 1~n, to find out the difference between the leftmost and the right position of each number that can be reached when simulating the bubble sort. Violence to find the law, you will find a number of the right shift is the number of the right is smaller than the number of numbers, so the right position is the current position plus a number smaller than the number of numbers. The left position is the smaller of the post-order position and the first position. The number of numbers that are smaller than this number in the right-hand side is a tree-like array, similar to the practice of petition example La 4329.
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e5 +Ten;intC[MAXN];intN;inlineintLowbit (intx) {returnX & (-x);}intSumintx) {intRET =0; while(X >0) {ret+ = C[x]; X-=lowbit (x); } returnret;}voidAddintXintd) { while(x <=MAXN) {C[x]+ = D; X + =lowbit (x); }}intA[MAXN];intR[MAXN], L[MAXN];intID[MAXN];intMain () {intT; scanf ("%d", &t); intCAS =0; while(t--) {memset (C,0,sizeof(C)); scanf ("%d", &N); for(inti =1; I <= N; i++) scanf ("%d", &A[i]); for(inti = n; I >=1; i--) {Add (A[i],1); R[i]= SUM (A[i]-1); Id[a[i]]=i; } //for (int i = 1; I <= n; i++) printf ("%d", r[i]); Puts ("");vector<int>ans; for(inti =1; I <= N; i++) { intLL =min (id[i], i); intrr = Id[i] +R[id[i]]; //printf ("Check%d%d%d\n", I, LL, RR);Ans.push_back (RR-ll); } printf ("Case #%d:", ++CAs); for(inti =0; I < ans.size (); i++) {printf ("%d", Ans[i]); }puts (""); }}
HDU 5775 Bubble Sort (tree-like array)