【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

迭代加深搜。
很容易想到,最多只要搜8层就可以得到答案了
->最多8下肯定可以还原。
则枚举一下最大层数。然后做个深搜就好。
优化。
设0..n每个数字的后继不为a[i]+1的个数为cnt
则每次操作显然最多只能减少3个cnt..
可以画图分析一下。
则可以拿这个来做剪枝。。
然后插入的过程可以用链表来实现。

【代码】

/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
8.whatch out the detail input require
*/
#include <bits/stdc++.h>
using namespace std; const int N = 10; int n,a[N+5],l[N+5],r[N+5],templ[N+5],tempr[N+5],maxdep,ans = -1; void Move(int i,int j,int pos){
r[l[i]] = r[j];
l[r[j]] = l[i];
l[r[pos]] = j;
r[j] = r[pos]; r[pos] = i;
l[i] = pos;
} void dfs(int dep){
if (dep==maxdep){
for (int i = 0;i <= n;i++)
if (a[r[i]]!=a[i]+1)
return; if (ans==-1) ans = dep;
else ans = min(ans,dep); return;
} int cnt = 0;
for (int i = 0;i <= n;i++)
if (a[r[i]]!=a[i]+1)
cnt++;
int delta = maxdep-dep;
if (delta*3<cnt) return; for (int i = r[0];i != n+1;i=r[i])
for (int j = i;j != n+1;j = r[j]){
//1,2,3..i..j,j+1....n
for (int pos = 0;pos!=l[i];pos=r[pos]){
int pre = l[i];
Move(i,j,pos);
dfs(dep+1);
Move(i,j,pre);
}
for (int pos = r[j];pos != n+1;pos = r[pos]){
int pre = l[i];
Move(i,j,pos);
dfs(dep+1);
Move(i,j,pre);
}
}
return;
} int main(){
#ifdef LOCAL_DEFINE
freopen("F:\\c++source\\rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
int kase = 0;
while (cin >> n && n){
cout <<"Case "<<++kase<<": ";
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i <= n+1;i++) l[i] = i-1,r[i] = i+1;
r[0] = 1;
a[n+1] = n+1;
ans = -1;
for (maxdep = 0;maxdep <= 9;maxdep++){
dfs(0);
if (ans!=-1) break;
}
cout << ans << endl;
}
return 0;
}

最新文章

  1. UITableView编辑
  2. python排序算法的实现-冒泡
  3. Spring基础——一个简单的例子
  4. 【BZOJ】2463: [中山市选2009]谁能赢呢?(博弈论)
  5. JS之iframe中的窗口
  6. python3学习问题汇总
  7. C++ Primer----一个关于 vector 的有趣的问题
  8. vmware-workstation-11中centos-6.6安装
  9. iOS 面试题 总结
  10. HDU-4705 Y 树形DP
  11. 图解server端网络架构
  12. c# 使用OracleParameter,同时使用replace函数
  13. 在Mac OS X系统下 用dd命令将iso镜像写入u盘
  14. Oracle 12C 新特性之move (非分区表)table online
  15. OpenCV 矩形轮廓检测
  16. leetcode 链表类型题目解题总结
  17. VMware Workstation 15 Pro 永久激活密钥 下载
  18. 树莓派安装tensorflow1.11
  19. c/c++线性循环队列
  20. div遮盖,弹出层

热门文章

  1. C/C++(C++类型增强)
  2. 洛谷 P1964 【mc生存】卖东西
  3. Floodlight中 处理packetin消息的顺序(2)
  4. LightOJ 1070 Algebraic Problem (推导+矩阵高速幂)
  5. js---10时间类
  6. js---- localStorage的基本用法
  7. POJ 3170 线段树优化DP
  8. Apple iMac性能基准测试
  9. private SortedDictionary&lt;string, object&gt; Dic_values = new SortedDictionary&lt;string, object&gt;();
  10. deep-in-es6(一)