For n numbers, resize from large to small or from smallest to largest, the operands are not more than N, and each operation has at least one number in the correct position.
The lifting point also has a sequence of pros and cons to find the minimum number of exchanges.
In order to exchange the times with greedy thought, first 1 exchange to 1, and then 2 Exchange to 2 ...
AC Code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring > #include <string> #include <sstream> #include <vector> #include <set> #include <map># Include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert > #include <cmath> #include <functional>using namespace std;const int maxn = 505;int a[1005], b[1005];int N; int solve () {int ret = 0xfffffff;for (int i = 0, j = n; i < n; i++, J + +) {A[j] = a[i];} for (int i = 0; i < n; i++) {int MARK[MAXN], cnt = 0;for (int j = 0; J < N; j + +) {mark[a[i + j]] = i + j;b[i + j] = A[i + j];} for (int j = 0; J < N; j + +) {if (b[i + j]! = j) {Cnt++;int idx = Mark[j];swap (B[i + j], B[idx]); MARK[B[IDX]] = idx;}} ret = min (ret, CNT);} return ret;} int main () {Ios::sync_with_stdio (false), while (CIN >> n && N) {for (int i = 0; i < n; i++) {cin >> a[i];a[i]--; Easy handling}int Ret;ret = Solve (); Reverse (A, a + N); Reverse and calculate once to find minimum ret = min (ret, solve ()); cout << ret << Endl;} return 0;}
Uva-10570-meeting with Aliens