冒泡排序强无敌
Orz w_yqts
爆搜剪枝
#include <cstdio>
#include <algorithm>
using namespace std;
short a[51],b[51],n,tot,l,r;
inline void rotate(int l,int r)
{
while (l<r) swap(a[l++],a[r--]);
}
bool dfs(int deep)
{
if (tot+(a[n]!=n)>deep) return 0;
if (!deep && a[1]==1 && !tot) return 1;
for (int i=n;i>1;--i)
if (i==n || abs(a[i]-a[i+1])>1)
{
if (i!=n) tot-=(abs(a[i]-a[i+1])>1);
rotate(1,i);
if (i!=n) tot+=(abs(a[i]-a[i+1])>1);
if (dfs(deep-1)) return 1;
if (i!=n) tot-=(abs(a[i]-a[i+1])>1);
rotate(1,i);
if (i!=n) tot+=(abs(a[i]-a[i+1])>1);
}
return 0;
}
int main()
{
int i,j,ans=0;
scanf("%d",&n);
for (i=1;i<=n;++i) scanf("%d",&a[i]),b[i]=a[i];
for (int i=1;i<n;++i) for (int j=i+1;j<=n;++j) if (b[i]>b[j]) swap(b[i],b[j]);
for (i=1;i<=n;++i) {for (j=1;j<=n;++j) if (a[i]==b[j]) a[i]=j;}
for (i=1;i<n;++i) tot+=abs(a[i]-a[i+1])>1;
for (;ans<=(n<<1)-2;++ans) if (dfs(ans)) break;
printf("%d\n",ans);
return 0;
}