Bubble Sort
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 173 Accepted Submission(s): 119
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.
7
2 6 5 7 4 1 3
2 6 5 7 1 4 3
2 6 5 1 7 4 3
2 6 1 5 7 4 3
2 1 6 5 7 4 3
1 2 6 5 7 4 3
1 2 6 5 7 3 4
1 2 6 5 3 7 4
1 2 6 3 5 7 4
1 2 3 6 5 7 4
1 2 3 6 5 4 7
1 2 3 6 4 5 7
1 2 3 4 6 5 7
1 2 3 4 5 6 7
打一下一个较长序列的运行过程,发现对于一个数,可以分为两类去看:
一种是类似7这个数,它的初始位置是4,整个过程中都是要往后移动的,7的后面有三个比它小的数字4,1,3,也就是说7在冒泡的过程中,最远向右偏移3个位置;
还有一种数字是类似于4这个数字,它初始位置是5,它要最终移动到位置4,最多左移 5 - 4 = 1个位置,4这个数字右面有两个比它小的数字1,3,也就是说在移动过程中向右最远移动2位,所以数字4的移动范围是1 + 2 = 3;
剩下的就用线段树来求一个数后面有多少比它小的数字,跟求逆序数很类似
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
const int maxn = 100005;
int sum[maxn << 2], re[maxn];
int x[maxn], index[maxn];
void PushUP(int rt) {
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void build(int l, int r, int rt) {
sum[rt] = 0;
if (l == r) return;
int m = (l + r) >> 1;
build(lson);
build(rson);
}
void update(int p, int l, int r, int rt) {
if (l == r) {
sum[rt]++;
return;
}
int m = (l + r) >> 1;
if (p <= m) update(p, lson);
else update(p, rson);
PushUP(rt);
}
int query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) {
return sum[rt];
}
int m = (l + r) >> 1;
int ret = 0;
if (L <= m) ret += query(L, R, lson);
if (R > m) ret += query(L, R, rson);
return ret;
}
int main()
{
int T, N;
cin >> T;
for (int cas = 1; cas <= T; cas++) {
scanf("%d", &N);
build(1, N, 1);
for (int i = 1; i <= N; i++) {
int tmp;
scanf("%d", &tmp);
index[tmp] = i;
x[i] = tmp;
}
for (int i = N; i >= 1; i--) {
re[x[i]] = query(1, x[i], 1, N, 1);
update(x[i], 1, N, 1);
}
printf("Case #%d:", cas);
for (int i = 1; i <= N; i++) {
if (index[i] <= i)
printf(" %d", re[i]);
else {
printf(" %d", re[i] + abs(index[i] - i));
}
}
puts("");
}
return 0;
}