北京网络赛G BOXES 状态压缩+有序BFS+高维数组判重
题目描述
Description
There is a strange storehouse in PKU. In this storehouse there are n slots for boxes, forming a line. In each slot you can pile up any amount of boxes. The limitation is that you can only pile a smaller one above a bigger one, in order to keep balance. The slots are numbered from 1 to n. The leftmost one is slot 1.
At first there is exactly one box in each slot. The volume of the box in slot i is vi. As a virgo, you decide to sort these boxes by moving some of them. In each move you can choose a slot and move the top box in this slot to an adjacent slot (of course you can only put it on the top). You should ensure that the limitation mentioned above is still satisfied after this move. After the sort operation, there should be exactly one box in each slot, and for each pair of adjacent slots, the box in the left one should be smaller than the box in the right one.
Your task is to calculate the minimum number of moves you need to sort the boxes.
Input
In the first line there’s an integer T(T≤6000), indicating the number of test cases. The following 2T lines describe the test cases.
In each test case, the first line contains an integer n, indicating the number of slots. The second line contains n integers v1,v2…vn, indicating the volume of the boxes. It is guaranteed that all vi in a test case are different.
Please note that n<8,0≤vi≤104
Output
For each test case, print a line containing one integer indicating the answer. If it’s impossible to sort the boxes, print -1.
- Sample Input
-
4
3
2 1 3
2
7 8
2
10000 1000
3
97 96 95 - Sample Output
-
4
0
-1
20#include <bits/stdc++.h>
using namespace std;
int vis1[];
int vis2[][];
int vis3[][][];
int vis4[][][][];
int vis5[][][][][];
int vis6[][][][][][];
int vis7[][][][][][][];
struct node{
int a[];
int step;
};
map<int,int> Hash;
int a[],b[],c[];
bool check(int a[],int num,int step){
if(num==){
if(vis1[a[]]>=) return true;
else{vis1[a[]]=step;return false;}
}
if(num==){
if(vis2[a[]][a[]]>=) return true;
else {vis2[a[]][a[]]=step;return false;}
}
if(num==){
if(vis3[a[]][a[]][a[]]>=) return true;
else {vis3[a[]][a[]][a[]]=step;return false;}
}
if(num==){
if(vis4[a[]][a[]][a[]][a[]]>=) return true;
else {vis4[a[]][a[]][a[]][a[]]=step;return false;}
}
if(num==){
if(vis5[a[]][a[]][a[]][a[]][a[]]>=) return true;
else {vis5[a[]][a[]][a[]][a[]][a[]]=step;return false;}
}
if(num==){
if(vis6[a[]][a[]][a[]][a[]][a[]][a[]]>=) return true;
else {vis6[a[]][a[]][a[]][a[]][a[]][a[]]=step;return false;}
}
if(num==){
if(vis7[a[]][a[]][a[]][a[]][a[]][a[]][a[]]>=) return true;
else {vis7[a[]][a[]][a[]][a[]][a[]][a[]][a[]]=step;return false;}
}
return false;
}
void init(){
memset(vis1,-,sizeof(vis1));
memset(vis2,-,sizeof(vis2));
memset(vis3,-,sizeof(vis3));
memset(vis4,-,sizeof(vis4));
memset(vis5,-,sizeof(vis5));
memset(vis6,-,sizeof(vis6));
memset(vis7,-,sizeof(vis7));
}
void bfs(int a[],int num){
node k;k.step=;
for(int i=;i<num;i++){k.a[i]=i;}
check(a,num,);
queue<node> que;que.push(k);
int b[];
while(!que.empty()){
node top=que.front();que.pop();
for(int i=;i<num;++i)
b[i]=top.a[i];
for(int i=;i<num;++i){
int l=b[i]-;
int r=b[i]+;
if(l<) l=-;
if(r>=num) r=-;
for(int j=;j<i;++j) if(b[j]==b[i]){l=r=-;}
for(int j=;j<i;++j) if(b[j]==b[i]-) {l=-;}
for(int j=;j<i;++j) if(b[j]==b[i]+) {r=-;}
if(l!=-){
int temp=b[i];
b[i]=l;
node s;
for(int j=;j<num;++j) {s.a[j]=b[j];}
s.step=top.step+;
if(!check(b,num,s.step)){
que.push(s);
}
b[i]=temp;
}
if(r!=-){
int temp=b[i];
b[i]=r;
node s;
for(int j=;j<num;++j) {s.a[j]=b[j];}
s.step=top.step+;
if(!check(b,num,s.step)){
que.push(s);
}
b[i]=temp;
}
}
}
} int main(){
int T,n;
for(int i=;i<;i++){
a[i]=i;
}
init();
for(int i=;i<=;i++){
bfs(a,i);
}
for(scanf("%d",&T);T;--T){
scanf("%d",&n);Hash.clear();
vector<int> Q;
for(int i=;i<n;i++){
scanf("%d",b+i);
Q.push_back(b[i]);
}
sort(Q.begin(),Q.end());
for(int i=;i<(int)Q.size();i++){
Hash[Q[i]]=i;
}
for(int i=;i<n;i++){
c[Hash[b[i]]]=i;
}
if(n==){
if(c[]==){
printf("0\n");
}
else printf("%d\n",vis1[c[]]);
}
if(n==){
if(c[]==&&c[]==){
printf("0\n");
}
else printf("%d\n",vis2[c[]][c[]]);
}
if(n==){
if(c[]==&&c[]==&&c[]==){
printf("0\n");
}
else printf("%d\n",vis3[c[]][c[]][c[]]);
}
if(n==){
if(c[]==&&c[]==&&c[]==&&c[]==){
printf("0\n");
}
else printf("%d\n",vis4[c[]][c[]][c[]][c[]]);
}
if(n==){
if(c[]==&&c[]==&&c[]==&&c[]==&&c[]==){
printf("0\n");
}
else printf("%d\n",vis5[c[]][c[]][c[]][c[]][c[]]);
}
if(n==){
if(c[]==&&c[]==&&c[]==&&c[]==&&c[]==&&c[]==){
printf("0\n");
}
else printf("%d\n",vis6[c[]][c[]][c[]][c[]][c[]][c[]]);
}
if(n==){
if(c[]==&&c[]==&&c[]==&&c[]==&&c[]==&&c[]==&&c[]==){
printf("0\n");
}
else printf("%d\n",vis7[c[]][c[]][c[]][c[]][c[]][c[]][c[]]);
}
}
return ;
}首先对之前搜到这个题解的人说声对不起,因为之前写的有问题也没修补
好了我来解释一下代码的关键部分。。。
好的,这其实是个sb题,没睡醒的同学可以看一下下面的解释。。。
首先是状态定义
a[i]:第i大的元素在值为a[i]的位置上
然后,我们可以离线地做这道题
初始状态有序让a[i]=i
然后让他去搜索所有有效的状态,因为你正着搜结果和结果往乱了推其实值是一样的,这是一个可逆的过程
然后我们使用bfs爆搜所有从有序状态出发的状态并且记录答案
bfs中的关键判断是
1,这个第i大的元素,上面有没有东西
检查方法,遍历0~i-1大的元素,看一下这些元素里有没有和它在同一个位置
2,相邻的右边的柱子能不能放东西
检查方法,遍历0~i-1大的元素,看一下这些元素有没有放到a[i]+1这个槽里,只要有那么至少是不能放的,因为只可能更小
3,相邻的左边的柱子能不能放东西
检查方法,遍历0~i-1大的元素,看一下这些元素有没有放到a[i]-1这个槽里,只要有那么至少是不能放的,因为只可能更小
然后就是复制状态,检查访问过没有,判断一下加入队列
然后就是一些细节的东西
数组下标什么的千万别搞混。。。离散化之后记得维护c[i]的意义,千万别直接扔个rank数组进去...
北京网络赛G BOXES 状态压缩+有序BFS+高维数组判重的更多相关文章
- 2015北京网络赛 G Boxes BFS+打表
G Boxes 题意:n个位置摆有n个箱子,每次移动只能把相邻的垒起来,且上面的必须小于下面的.求摆成升序需要移动多少步. 思路:这里的n很小,只有7.但是bfs最快的情况需要2s左右,所以就打表了. ...
- 北京网络赛G BOXES 大模拟+BFS
题目描述 Description There is a strange storehouse in PKU. In this storehouse there are n slots for boxe ...
- 2015北京网络赛 G题 Boxes bfs
Boxes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingonl ...
- hihocode 1584 : Bounce (找规律)(2017 北京网络赛G)
题目链接 比赛时随便找了个规律,然后队友过了.不过那个规律具体细节还挺烦的.刚刚偶然看到Q巨在群里提到的他的一个思路,妙啊,很好理解,而且公式写起来也容易.OrzQ巨 #include<bits ...
- hdu 5038 (2014北京网络赛G 排序水题)
题意:有n个数字,带入10000 - (100 - ai) ^ 2公式得到n个数,输出n个数中频率最大的数,如果有并列就按值从小到大都输出输出,如果频率相同的数字是全部的n个数,就输出Bad....题 ...
- 2018北京网络赛 G The Mole /// 分块暴力 点线距离
题目大意: 给定n段线段 编号为1~n 接下来m个询问 给定一点 输出离该点最近的线段的最小编号(距离相等时取编号小的) 题解 大致就是 1.坐标范围为(0,2^16-1) 将坐标系划分为2^8*2^ ...
- 2015 北京网络赛 E Border Length hihoCoder 1231 树状数组 (2015-11-05 09:30)
#1231 : Border Length 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Garlic-Counting Chicken is a special spe ...
- ACM-ICPC2018北京网络赛 Saving Tang Monk II(bfs+优先队列)
题目1 : Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 <Journey to the West>(also < ...
- 2015北京网络赛 J Scores bitset+分块
2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...
随机推荐
- heap和stack有什么区别
1.heap是堆,stack是栈. 2.stack的空间由操作系统自动分配和释放,heap的空间是手动申请和释放的,heap常用new关键字来分配. 3.stack空间有限,heap的空间是很大的自由 ...
- SOA面向服务架构简述
在上篇中我们简单谈了下架构设计中服务层的简单理解,在这里我们将继续服务层的架构,在本节我们将重点在于分布式服务.在分布式系统中表现层和业务逻辑层 并不处于同一物理部署,所以我们必须存在分布式服务,以契 ...
- 操作sqlserver数据库常用的三个方法
1. ADO.NET -> 连接字符串,常用的两种方式: server=计算机名或ip\实例名;database=数据库名;uid=sa;pwd=密码; server=计算机名或ip\实例名;d ...
- editplus批量删除重复行(编辑-删除-删除重复行)
editplus快速删除重复数据 多行文本,有些行的文字或数据是重复的,该怎么删除重复部分,只留下不重复的部分?很多人对这个问题感到无比头疼,Editplus同样能快速帮你删除数据. 那么,editp ...
- The Pilots Brothers' refrigerator(dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19718 ...
- Spring常用的接口和类(三)
一.CustomEditorConfigurer类 CustomEditorConfigurer可以读取实现java.beans.PropertyEditor接口的类,将字符串转为指定的类型.更方便的 ...
- Java中的构造函数和重载
一.Java中的构造函数 构造函数是对象被创建时初始化对象的成员方法,它具有和它所在的类完全一样的名字.构造函数只能有入口参数,没有返回类型,因为一个类的构造方法的返回类就是类本身.构造函数定义后,创 ...
- 2012年湖南省程序设计竞赛E题 最短的名字
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1115 解题报告:输入n个字符串,让你求出可以用来区别这些字符串的最少的前缀总共有多少个字 ...
- 消息通信库ZeroMQ 4.0.4安装指南
一.ZeroMQ介绍 ZeroMQ是一个开源的消息队列系统,按照官方的定义,它是一个消息通信库,帮助开发者设计分布式和并行的应用程序. 首先,我们需要明白,ZeroMQ不是传统的消息队列系统(比如Ac ...
- GDB 使用大法
一.GDB 我用的是 GCC+POWERSHELL+GDB, GDB刚刚接触也有很多要记的. 二.一个调试示例 tst.c #include <stdio.h> int func(int ...