题意:输入一个序列,每次可以交换两个整数,用最少的交换次数,使得序列变成一个环状序列
分析:看了网上大多数都是暴力,列举1到n分别作为开头,分别正序和逆序。
#include<iostream>
#include<string.h>
#include<sstream>
#include<set>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<math.h>
using namespace std;
int n;
int A0[500 + 10], D0[500 + 10];
int solve(int k, int f) {
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (A0[i] != k) {
A0[D0[k]] = A0[i];
D0[A0[i]] = D0[k];
D0[k] = i;
A0[i] = k;
cnt++;
}
k += f;
if (k > n)k = 1;
if (k <= 0)k = n;
}
return cnt;
}
int main() {
int A[500 + 10];
int D[500 + 10];
while (cin>>n&&n) {
for (int i = 1; i <= n; i++) { cin >> A[i]; D[A[i]] = i; }
int ans = 5000;
for (int i = 1; i <= n; i++) {
memcpy(A0, A, sizeof(A));
memcpy(D0, D, sizeof(D));
ans = min(ans, solve(i, 1));
memcpy(A0, A, sizeof(A));
memcpy(D0, D, sizeof(D));
ans = min(ans, solve(i, -1));
}
cout << ans << endl;
}
return 0;
}