2015北京赛区网络赛 Boxes
2015-09-21 21:34
429 查看
状压 bfs
预处理所有的情况(样例数为6000,太多,所以预处理)
先离散化一次
状态的表示为数字t
数字t的第i个数字上的数字为x,数字i在x位置上
用mp数组来存每个状态t
mp[t]表示t到终止状态的步数
记住要用数组来存,不要用map,因为的存的数字过多,map的查询不是o(n)的时间
要用数组
预处理所有的情况(样例数为6000,太多,所以预处理)
先离散化一次
状态的表示为数字t
数字t的第i个数字上的数字为x,数字i在x位置上
用mp数组来存每个状态t
mp[t]表示t到终止状态的步数
记住要用数组来存,不要用map,因为的存的数字过多,map的查询不是o(n)的时间
要用数组
#include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <cstring> #include <climits> #include <string> #include <vector> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <sstream> #include <cctype> using namespace std; typedef long long ll; typedef pair<int ,int> pii; #define MEM(a,b) memset(a,b,sizeof a) #define CLR(a) memset(a,0,sizeof a); const int inf = 0x3f3f3f3f; const int MOD = 1e9 + 7; //#define LOCAL int End; int a[10]; int b[10]; int mp[10000000]; map<int,int> mmp; void init(){ int t=0; for(int i=1;i<=7;i++){ t = 0; for(int j=1;j<=i;j++){ t += pow(10,i-j)*j; } queue<int> q; q.push(t); mp[t]=1; while(!q.empty()){ int x = q.front(); q.pop(); int top[10]; MEM(top,inf); int a[10]; int cnt = i; int re = x; while(re){ a[cnt--] = re%10; re/=10; } for(int j=1;j<=i;j++){ top[a[j]]=min(top[a[j]],j); } for(int j=1;j<=i;j++){ if(j-1>=1 && (top[j]<top[j-1] || top[j-1]==inf) && top[j]!=inf){ int s=0; for(int t=1;t<=i;t++){ if(t==top[j]){ s = s*10 + j-1; } else s = s*10+a[t]; } if(!mp[s]){ mp[s]=mp[x]+1; q.push(s); } } if(j+1<=i && (top[j]<top[j+1] || top[j+1]==inf) && top[j]!=inf){ int s=0; for(int t=1;t<=i;t++){ if(t==top[j]){ s = s*10 + j+1; } else s = s*10+a[t]; } if(!mp[s]){ mp[s]=mp[x]+1; q.push(s); } } } } } } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); // freopen("out.txt","w",stdout); #endif init(); int t;cin >> t; while(t--){ End = 0; int n;cin >> n; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); b[i]=a[i]; } sort(a+1,a+1+n); for(int i=1;i<=n;i++){ mmp[a[i]]=i; } for(int i=1;i<=n;i++){ b[i]=mmp[b[i]]; } for(int i=1;i<=n;i++){ End+=(int)pow(10,n-b[i])*i; } printf("%d\n",mp[End]-1); } return 0; }
相关文章推荐
- /etc/profile和 . profile 文件 http://blog.csdn.net/louiswang2009/article/details/5886575
- hdu5461(2015沈阳网络赛L题)
- main函数调用、进程终止以及命令行参数和环境表http://www.tuicool.com/articles/FNnMNr
- UNIX环境编程学习笔记(16)——进程管理之进程环境变量 http://www.cnblogs.com/lienhua34/p/4005367.html
- hdu5452(2015沈阳网络赛C题)
- hdu5455(2015沈阳网络赛F题)
- hdu5459(2015沈阳网络赛J题)
- 农村借贷会是网络巨头的下一个角斗场吗?
- TCP客户/服务器程序示例
- iOS9 HTTP 不能正常使用的解决办法
- 常见的HTTP状态码
- Android请求网络共通类——Hi_博客 Android App 开发笔记
- centos 7 网络配置( 网关、dns、ip地址配置)
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛 B Mission Impossible 6
- Linux环境变量的设置和查看方法 http://soft.chinabyte.com/os/169/11412169.shtml
- ExtJS4 Ext.onReady、Ext.define、Ext.create的区别与联系 http://blog.sina.com.cn/s/blog_48d7f92901011cfn.html
- extjs组件添加事件监听的三种方式 http://blog.sina.com.cn/s/blog_48d7f92901011cfn.html
- 2015北京网络赛题目: Fractal(打表+二分模拟)
- linux常用c函数 进程操作篇 http://blog.chinaunix.net/uid-25906157-id-3136757.html
- TCP协议:服务端和客户端demo--【J2SE】