# BZOJ 2824: [AHOI2012]铁盘整理

Time Limit: 10 Sec
Memory Limit: 128 MB

Description

Input

Output

Sample Input

5

2 4 3 5 1

Sample Output

5

#### Code

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<vector>
using namespace std;
#define PA pair<int,int>
int read()
{
int s=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-'0';ch=getchar();}
return s*f;
}
//smile please
int n,a[51];
int ls[105],ans,sum,pr;
void dfs()
{
if(sum+pr>=ans)return;
if(pr==0&&a[1]==1)
{ans=sum;return;}
for(int i=2;i<=n;i++)
{
if(i!=n)pr+=(abs(a[1]-a[i+1])!=1)-(abs(a[i]-a[i+1])!=1);
for(int j=1;j<i+1-j;j++)
swap(a[j],a[i-j+1]);
++sum;
dfs();
--sum;
for(int j=1;j<i+1-j;j++)
swap(a[j],a[i-j+1]);
if(i!=n)pr-=(abs(a[1]-a[i+1])!=1)-(abs(a[i]-a[i+1])!=1);
}
}
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
n=read();
for(int i=1;i<=n;i++)a[i]=read(),ls[a[i]]++;
for(int i=1;i<=100;i++)ls[i]+=ls[i-1];
for(int i=1;i<=n;i++)a[i]=(ls[a[i]]--);
ans=n*2-2;
for(int i=1;i<n;i++)
if(abs(a[i+1]-a[i])!=1)
pr++;
dfs();
printf("%d\n",ans);
//fclose(stdin);
//fclose(stdout);
return 0;
}


posted on 2016-03-19 16:29 wuyuhan 阅读(...) 评论(...) 编辑 收藏