參考程序:
#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;int main() {int n, ans = 0;// 讀取學生人數scanf("%d", &n);// 用 vector 存儲每個學生的身高和體重(h, w)vector<pair<int, int>> a(n);for (int i = 0; i < n; i++)scanf("%d%d", &a[i].first, &a[i].second); // 讀入每個學生的 h 和 w// 枚舉所有 i < j 的學生對for (int i = 0; i < n; i++)for (int j = i + 1; j < n; j++)// 如果 a[i] < a[j],說明 i 應該排在 j 的后面// 但現在在前面,表示出現了“逆序對”,需要一次交換if (a[i] < a[j])ans++;// 輸出最少交換次數cout << ans << '\n';return 0;
}