題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=235
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <iostream> #include <vector> #include <algorithm>
using namespace std;
int sorting(vector<int> &v){ int size = v.size(); int count = 0; for(int i=0; i<size-1; ++i){ for(int j=i; j<size; ++j){ if(v[i] > v[j]){ swap(v[i], v[j]); count++; } } } return count; }
int main(){ int l; cin >> l; while(l--){ int n; cin >> n; vector<int> v(n); for(int i=0; i<n; ++i){ cin >> v[i]; } int ans = sorting(v); cout << "Optimal train swapping takes " << ans << " swaps.\n"; } }
|