Boxes

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://hihocoder.com/contest/acmicpc2015beijingonline/problem/7

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 there are infinity common points, 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

HINT

题意

给你一堆盒子,每个盒子只能仍在旁边比他大的上面,你每次只能操作当前位置最小的一个

然后问你最少多少次,可以使得盒子摆放有序

题解:

bfs爆搜……

恶心题,太麻烦了= =

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000000 + 500
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
//************************************************************************************** int dp7[][][][][][][];
int vis7[][][][][][][];
int dp6[][][][][][];
int vis6[][][][][][];
int dp5[][][][][];
int vis5[][][][][];
int dp4[][][][];
int vis4[][][][];
int dp3[][][];
int vis3[][][];
int dp2[][];
int vis2[][];
int dp1[];
int vis1[];
map<int,int> H;
int a[];
int b[];
int vvv[];
struct node
{
int a[];
int step;
};
int check(int a[],int num,int step)
{
if(num==)
{
if(vis7[a[]][a[]][a[]][a[]][a[]][a[]][a[]])
return ;
dp7[a[]][a[]][a[]][a[]][a[]][a[]][a[]]=step;
vis7[a[]][a[]][a[]][a[]][a[]][a[]][a[]] = ;
return ;
}
if(num==)
{
if(vis6[a[]][a[]][a[]][a[]][a[]][a[]])
return ;
dp6[a[]][a[]][a[]][a[]][a[]][a[]]=step;
vis6[a[]][a[]][a[]][a[]][a[]][a[]]= ;
return ;
}
if(num==)
{
if(vis5[a[]][a[]][a[]][a[]][a[]])
return ;
dp5[a[]][a[]][a[]][a[]][a[]]=step;
vis5[a[]][a[]][a[]][a[]][a[]]= ;
return ;
}
if(num==)
{
if(vis4[a[]][a[]][a[]][a[]])
return ;
dp4[a[]][a[]][a[]][a[]]=step;
vis4[a[]][a[]][a[]][a[]]= ;
return ;
}
if(num==)
{
if(vis3[a[]][a[]][a[]])
return ;
dp3[a[]][a[]][a[]]=step;
vis3[a[]][a[]][a[]]= ;
return ;
}
if(num==)
{
if(vis2[a[]][a[]])
return ;
dp2[a[]][a[]]=step;
vis2[a[]][a[]]= ;
return ;
}
if(num==)
{
if(vis1[a[]])
return ;
dp1[a[]]=step;
vis1[a[]]= ;
return ;
}
return ;
}
void solve(int ccc[],int step,int num)
{
node p;
for(int i=;i<num;i++)
p.a[i]=ccc[i];
int a[];
check(ccc,num,);
queue<node> Q;
p.step = ;
Q.push(p);
while(!Q.empty())
{
node k = Q.front();
Q.pop();
for(int i=;i<num;i++)
a[i]=k.a[i];
for(int i=;i<num;i++)
{
int temp = a[i];
int l = a[i]-,r = a[i]+;
if(l<)l=-;
if(r>=num)r=-;
for(int j=;j<i;j++)
{
if(a[j]==a[i])
{
l = -;
r = -;
}
}
for(int j=;j<i;j++)
if(a[j]==a[i]-)
l = -;
for(int j=;j<i;j++)
if(a[j]==a[i]+)
r = -;
if(l!=-)
{
a[i]=l;
for(int i=;i<num;i++)
p.a[i]=a[i];
p.step = k.step + ;
if(check(a,num,p.step))
Q.push(p);
a[i]=temp;
}
if(r!=-)
{
a[i]=r;
for(int i=;i<num;i++)
p.a[i]=a[i];
p.step = k.step + ;
if(check(a,num,p.step))
Q.push(p);
a[i]=temp;
}
}
}
} int main()
{
int t;scanf("%d",&t);
int c[];
memset(dp7,-,sizeof(dp7));
memset(dp6,-,sizeof(dp6));
memset(dp5,-,sizeof(dp5));
memset(dp4,-,sizeof(dp4));
memset(dp3,-,sizeof(dp3));
memset(dp2,-,sizeof(dp2));
memset(dp1,-,sizeof(dp1));
for(int i=;i<;i++)
c[i]=i;
for(int i=;i<;i++)
solve(c,,i+);
while(t--)
{
H.clear();
vector<int> Q;
int n;scanf("%d",&n);
for(int i=;i<n;i++)
{scanf("%d",&a[i]);Q.push_back(a[i]);}
sort(Q.begin(),Q.end());
for(int i=;i<Q.size();i++)
H[Q[i]]=i;
for(int i=;i<n;i++)
b[H[a[i]]]=i;
if(n==)
{
if(b[]==)
printf("0\n");
else
printf("%d\n",dp1[b[]]);
}
if(n==)
{
if(b[]==&&b[]==)
printf("0\n");
else
printf("%d\n",dp2[b[]][b[]]);
}
if(n==)
{
if(b[]==&&b[]==&&b[]==)
printf("0\n");
else
printf("%d\n",dp3[b[]][b[]][b[]]);
}
if(n==)
{
if(b[]==&&b[]==&&b[]==&&b[]==)
printf("0\n");
else
printf("%d\n",dp4[b[]][b[]][b[]][b[]]);
}
if(n==)
{
if(b[]==&&b[]==&&b[]==&&b[]==&&b[]==)
printf("0\n");
else
printf("%d\n",dp5[b[]][b[]][b[]][b[]][b[]]);
}
if(n==)
{
if(b[]==&&b[]==&&b[]==&&b[]==&&b[]==&&b[]==)
printf("0\n");
else
printf("%d\n",dp6[b[]][b[]][b[]][b[]][b[]][b[]]);
}
if(n==)
{
if(b[]==&&b[]==&&b[]==&&b[]==&&b[]==&&b[]==&&b[]==)
printf("0\n");
else
printf("%d\n",dp7[b[]][b[]][b[]][b[]][b[]][b[]][b[]]);
} }
}

2015北京网络赛 G题 Boxes bfs的更多相关文章

  1. 2015北京网络赛 G Boxes BFS+打表

    G Boxes 题意:n个位置摆有n个箱子,每次移动只能把相邻的垒起来,且上面的必须小于下面的.求摆成升序需要移动多少步. 思路:这里的n很小,只有7.但是bfs最快的情况需要2s左右,所以就打表了. ...

  2. 2015北京网络赛 H题 Fractal 找规律

    Fractal Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingo ...

  3. hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题

    题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给 ...

  4. 2015北京网络赛B题 Mission Impossible 6

    借用大牛的一张图片:模拟 #include<cstdio> #include<cmath> #include<cstring> #include<algori ...

  5. 2015北京网络赛A题The Cats' Feeding Spots

    题意:给你一百个点,找个以这些点为中心的最小的圆,使得这个圆恰好包含了n个点,而且这个圆的边界上并没有点 解题思路:暴力枚举每个点,求出每个点到其他点的距离,取第n大的点,判断一下. #include ...

  6. 2015北京网络赛 A题 The Cats' Feeding Spots 暴力

    The Cats' Feeding Spots Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acm ...

  7. 2015北京网络赛 Couple Trees 倍增算法

    2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道.  解法来自 q ...

  8. 2015北京网络赛 D-The Celebration of Rabbits 动归+FWT

    2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x ...

  9. 2015北京网络赛 J Scores bitset+分块

    2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...

随机推荐

  1. java中synchronized的用法详解

    记下来,很重要. Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchron ...

  2. Webform——Repeater多表联合显示

    对于一个表里,通过外键连接如何显示另一个表的数据,前Winform里可以用封装类来实现. 对于Webform,可以用封装类,也可以用Repeater的ItemDataBound事件(//在项被绑定数据 ...

  3. jquery提示气泡

    <link href="css/manhua_hoverTips.css" type="text/css" rel="stylesheet&qu ...

  4. SHA-1加密

    /** * SHA-1加密 * @param strSrc 要加密的字符串 * @return 加密后的字符串 */ public static String SHAEncrypt(String st ...

  5. varchar与nvarchar的区别

    nvarchar可变长度的Unicode字符数据 varchar可变长度且非 Unicode 的字符数据 举例: varchar(1)   --可以插进入一个数字或者一个字母,如果要插入一个汉字改为v ...

  6. Js动态传递不定数目的参数

    回调程序中,经常有这样的需求:用户传递一个回调方法,该方法可以有不定的参数. 如果参数数目固定则很容易实现,看代码: //回调函数1 function callback1(a,b,c) { alert ...

  7. Kettle定时执行(ETL工具)【转】

    1,Kettle跨平台使用.    例如:在AIX下(AIX是IBM商用UNIX操作系统,此处在LINUX/UNIX同样适用),运行Kettle的相关步骤如下:    1)进入到Kettle部署的路径 ...

  8. POJ2115 C Looooops 模线性方程(扩展欧几里得)

    题意:很明显,我就不说了 分析:令n=2^k,因为A,B,C<n,所以取模以后不会变化,所以就是求(A+x*C)%n=B 转化一下就是求 C*x=B-A(%n),最小的x 令a=C,b=B-A ...

  9. ASP.Net MVC_DotNetZip简单使用方法,解决文件压缩的问题[转]

    准备工作: 在vs工具栏中找到NuGet   下载DotNetZip   现在就可以使用DotNetZip强大的类库了,在这里我给出一些简单的使用. ? 1 2 3 4 5 6 7 8 9 10 11 ...

  10. unity3d实现序列帧动画

    首先准备一个序列帧图片如下的AngryBird: 场景中随便创建一个物体,这里以Cube为例 将图片拖放到Cube上,这样会在Cube的6各面都有3个bird,为了美观显示一个鸟,我们调整材质的Til ...