题目链接:http://vjudge.net/problem/UVA-10570
大意:给你一个由1-n组成的环状序列,每次可以交换任意两个数,用最少的交换次数将其变为1-n的环状排列。
思路:可以直接枚举1所在的位置,对于每个位置,排列有顺时针排列和逆时针排列,分别计算一下即可。详见代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lc rt<<1
#define rc rt<<1|1
using namespace std;
typedef long long LL;
const int INF = 1e9 + 10;
const int maxn = 500 + 10;
int pos[maxn],arr[maxn],l[maxn],r[maxn],a[maxn],pos_a[maxn];
int n;
int solve_L(int p)
{
int ans = 0;
if (a[p] != 1) { //要将位置p置为1
int x = a[p];
swap(a[p], a[pos_a[1]]);
swap(pos_a[x], pos_a[1]);
++ans;
}
int cur = 1;
whil