Title: give you a sequence that can only exchange adjacent two numbers at a time, asking the minimum number of exchanges that make the sequence increment.
Analysis: Mathematics, reverse order number.
If a[i] > A[j] and I < J are present in a sequence, then a[i] and a[j] form a pair of reverse pairs;
The total number of reverse pairs of a sequence is the inverse number of the sequence;
Title, the adjacent element exchange, the reverse number of each exchange sequence will inevitably change 1, and an increment of the sequence number 0;
Therefore, the least number of exchanges is the number of reverse order, and each time in reverse order to reduce the way to get an incremental sequence of exchange.
Description: Finishing a long time ago brush the topic.
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio>using namespace std; int Data[100];int Main () {int n,m,count;while (CIN >> N) while (n--) {cin >> m;for (int i = 0; i < m; + + I cin >> Data[i];count = 0;for (int i = 0; i < m; + + i) for (int j = 0; J < i; + + j) Count + = (data[j]>dat A[i]) cout << "Optimal train swapping takes" << count << "swaps." << Endl; return 0;}
UVa 299-train Swapping