时/空限制:1s / 64MB
题解 :
- 模拟一遍,由于每次只能交换相邻两数,发现要达到从小到大排列的效果,最少交换次数就是每一个数前面逆序数数量之和
#include <iostream>
using namespace std;
const int N = 1e3 + 10;
int a[N], b[N];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++ i)
cin >> a[i];
int ans = 0;
for (int i = 1; i <= n; ++ i) {
int cnt = 0;
for (int j = 1; j < i; ++ j) {
if (a[j] > a[i])
cnt ++ ;
}
ans += cnt;
b[a[i]] = cnt;
}
for (int i = 1; i <= n; ++ i) {
cout << b[i] << ' ';
}
cout << endl << ans;
}