虽然说了水题再不做,不过是训练赛的题,我还是坐下吧。题目是直接随机生成的。
再说了,这题也不容易,想我英语四级都过了的人居然看不到的题。
还好看Sample,直觉上感觉是冒泡排序
结果真是
Description
求冒泡排序次数
Code
#include <cstdio>
#include <iostream>
using namespace std;
const int MAXN = 50 + 9;
int a[MAXN];
void solve()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int ans = 0;
for (int i = 0; i < n; i++) {
bool flag = true;
for (int j = 0; j < n - 1; j++) {
if (a[j] > a[j + 1]) {
swap(a[j], a[j + 1]);
ans++;
flag = false;
}
}
if (flag) break;
}
printf("Optimal train swapping takes %d swaps.\n", ans);
}
int main()
{
//freopen("in.txt", "r", stdin);
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
solve();
}
}