给出一个排序算法(用伪代码表示):
// 排序算法
SORT(A)
for i from 1 to n // n 是序列 A 的元素个数
for j from 1 to n
if a[i] < a[j] // a[i] 是序列 A 的第 i 个元素
Swap a[i] and a[j]
请你算出对于一个序列 A = a 1 , a 2 , ⋯ , a n A=a_1,a_2,\cdots,a_n A=a1,a2,⋯,an 的所有前缀 A k = a 1 , a 2 , ⋯ , a k A_k=a_1,a_2,\cdots,a_k Ak=a1,a2,⋯,ak( 1 ≤ k ≤ n 1\le k\le n 1≤k≤n), SORT ( A k ) \operatorname{SORT}(A_k) SORT(Ak) 中的交换(Swap)操作将会被执行几次。
Paimon just invents a new sorting algorithm which looks much like bubble sort \textit{bubble sort} bubble sort, with a few differences. It accepts a 1 1 1-indexed sequence A A A of length n n n and sorts it. Its pseudo-code is shown below.
// The Sorting Algorithm
SORT(A)
for i from 1 to n // n is the number of elements if A
for j from 1 to n
if a[i] < a[j] // a[i] is the i-th element in A
Swap a[i] and a[j]
If you don’t believe this piece of algorithm can sort a sequence it will also be your task to prove it. Anyway here comes the question:
Given an integer sequence A = a 1 , a 2 , ⋯ , a n A = a_1, a_2, \cdots, a_n A=a1,a2,⋯,an of length n n n, for each of its prefix A k A_k Ak of length k k k (that is, for each 1 ≤ k ≤ n 1 \le k \le n 1≤k≤n, consider the subsequence A k = a 1 , a 2 , ⋯ , a k A_k = a_1, a_2, \cdots, a_k Ak=a1,a2,⋯,ak), count the number of swaps performed if we call SORT ( A k ) \text{SORT}(A_k) SORT(Ak).
There are multiple test cases. The first line of the input contains an integer T T T indicating the number of test cases. For each test case:
The first line contains an integer n n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1≤n≤105) indicating the length of the sequence.
The second line contains n n n integers a 1 , a 2 , ⋯ , a n a_1, a_2, \cdots, a_n a1,a2,⋯,an ( 1 ≤ a i ≤ n 1 \le a_i \le n 1≤ai≤n) indicating the given sequence.
It’s guaranteed that the sum of n n n of all test cases will not exceed 1 0 6 10^6 106.
For each test case output one line containing n n n integers s 1 , s 2 , ⋯ , s n s_1, s_2, \cdots, s_n s1,s2,⋯,sn separated by a space, where s i s_i si is the number of swaps performed if we call SORT ( A i ) \text{SORT}(A_i) SORT(Ai).
Please, DO NOT output extra spaces at the end of each line or your solution may be considered incorrect!
3
5
2 3 2 1 5
3
1 2 3
1
1
0 2 3 5 7
0 2 4
0
以上由米哈游赞助 以上由米哈游赞助 以上由米哈游赞助(注意题目标题:Paimon?)
分类讨论
这么长题解不想看?那就给我看完。
#include
#define int long long
using namespace std;
const int Maxn = 1e5 + 5;
int n, a[Maxn];
int Tree[Maxn];
bool vis[Maxn];
inline int lowbit(int x) {
return x & (-x);
}
inline void Add(int x) {
for (int i = x; i <= n; i += lowbit(i)) {
Tree[i]++;
}
}
inline int Sum(int x) {
int tot = 0;
for (int i = x; i; i -= lowbit(i)) {
tot += Tree[i];
}
return tot;
}
inline void solve() {
memset(Tree, 0, sizeof(Tree));
memset(vis, 0, sizeof(vis));
cin >> n;
int res = 0, cnt = 0;
bool flag = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cout << 0;
vis[a[1]] = 1;
Add(a[1]);
for (int i = 2; i <= n; i++) {
if (!vis[a[i]]) {
vis[a[i]] = 1;
Add(a[i]);
}
if (a[i] == a[1]) {
flag = 1;
}
cnt += flag - (flag ? a[i] > a[1] : 0);
if (a[i] > a[1]) {
res += cnt + 1;
swap(a[1], a[i]);
cnt = flag = 0;
}
res += Sum(a[1]) - Sum(a[i]);
cout << " " << res;
}
cout << endl;
}
inline void work() {
int T;
cin >> T;
while (T--) {
solve();
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
work();
return 0;
}