Title Link: HDU 5775
Surface:
Bubble Sort
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 709 Accepted Submission (s): 418
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 Input
233 1 231) 2 3
Sample Output
Case #1:1 1 2Case #2:0 0 0Hintin first case, (3, 1, 2), (3, 1, 2)--(1, 3, 2)--(1, 2, 3) the LEFTM OST place and rightmost place of 1 was 1 and 2, 2 is 2 and 3, 3 was 1 and 3In second case, the array had already in Increasi Ng order. The answer of every number is 0.
Authorfzu
Source2016 multi-university Training Contest 4
Test instructions
Given a random arrangement of a 1-n, the sequence is bubbled up, asking the difference between the right and left end of each number that is reached in the sort process.
Solving:
Because the specific process of sorting is starting from the right, each time the current smallest value is moved to the leftmost end, a number will arrive at the rightmost position where it starts + its right side is less than its number (it will move so many steps to the right when it moves to the left in a smaller number). The leftmost position is the small of both its original position and the position it should be in after sorting. Finally, the two-position difference is the desired one.
The key point is how to count a number to the right of a few numbers smaller than it can be traversed from right to left, with a tree-like array maintenance, because it is the edge of mobile statistics, each time you can directly ask "1-a[j]-1" and, that is, the number of the right is less than a[j].
Code:
#include <iostream> #include <cstring> #include <cstdio> #define INF 1000000using namespace Std;int a[ 100010],c[100010],ans[100010];int t,n;int max (int a,int b) {return a>b?a:b;} int min (int a,int b) {return a<b?a:b;} int lowbit (int x) {return x& (-X);} int sum (int x) {int Res=0;while (x>0) {res+=c[x];x-=lowbit (x);} return res;} void Update (int i,int val) {while (i<=n) {c[i]+=val;i+=lowbit (i);}} int main () {int tmp,le,ri;scanf ("%d", &t); for (int i=1;i<=t;i++) {scanf ("%d", &n), for (int j=0;j<n;j++) scanf ("%d", &a[j]), memset (C,0,sizeof ( c)); for (int j=n-1;j>=0;j--) { tmp=sum (a[j]); Update (a[j],1); Le=min (a[j]-1,j); ri=j+tmp; Ans[a[j]]=ri-le;} printf ("Case #%d:", I), and for (int j=1;j<=n;j++) printf ("%d", Ans[j]);p rintf ("\ n");} return 0;}
HDU 5775 Bubble Sort (tree-like array)