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[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
//**************************************************************************************int dp7[7][7][7][7][7][7][7];
int vis7[7][7][7][7][7][7][7];
int dp6[7][7][7][7][7][7];
int vis6[7][7][7][7][7][7];
int dp5[7][7][7][7][7];
int vis5[7][7][7][7][7];
int dp4[7][7][7][7];
int vis4[7][7][7][7];
int dp3[7][7][7];
int vis3[7][7][7];
int dp2[7][7];
int vis2[7][7];
int dp1[7];
int vis1[7];
map<int,int> H;
int a[10];
int b[10];
int vvv[10];
struct node
{int a[7];int step;
};
int check(int a[],int num,int step)
{if(num==7){if(vis7[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]])return 0;dp7[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=step;vis7[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]] = 1;return 1;}if(num==6){if(vis6[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]])return 0;dp6[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]]=step;vis6[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]]= 1;return 1;}if(num==5){if(vis5[a[0]][a[1]][a[2]][a[3]][a[4]])return 0;dp5[a[0]][a[1]][a[2]][a[3]][a[4]]=step;vis5[a[0]][a[1]][a[2]][a[3]][a[4]]= 1;return 1;}if(num==4){if(vis4[a[0]][a[1]][a[2]][a[3]])return 0;dp4[a[0]][a[1]][a[2]][a[3]]=step;vis4[a[0]][a[1]][a[2]][a[3]]= 1;return 1;}if(num==3){if(vis3[a[0]][a[1]][a[2]])return 0;dp3[a[0]][a[1]][a[2]]=step;vis3[a[0]][a[1]][a[2]]= 1;return 1;}if(num==2){if(vis2[a[0]][a[1]])return 0;dp2[a[0]][a[1]]=step;vis2[a[0]][a[1]]= 1;return 1;}if(num==1){if(vis1[a[0]])return 0;dp1[a[0]]=step;vis1[a[0]]= 1;return 1;}return 0;
}
void solve(int ccc[],int step,int num)
{node p;for(int i=0;i<num;i++)p.a[i]=ccc[i];int a[10];check(ccc,num,0);queue<node> Q;p.step = 0;Q.push(p);while(!Q.empty()){node k = Q.front();Q.pop();for(int i=0;i<num;i++)a[i]=k.a[i];for(int i=0;i<num;i++){int temp = a[i];int l = a[i]-1,r = a[i]+1;if(l<0)l=-1;if(r>=num)r=-1;for(int j=0;j<i;j++){if(a[j]==a[i]){l = -1;r = -1;}}for(int j=0;j<i;j++)if(a[j]==a[i]-1)l = -1;for(int j=0;j<i;j++)if(a[j]==a[i]+1)r = -1;if(l!=-1){a[i]=l;for(int i=0;i<num;i++)p.a[i]=a[i];p.step = k.step + 1;if(check(a,num,p.step))Q.push(p);a[i]=temp;}if(r!=-1){a[i]=r;for(int i=0;i<num;i++)p.a[i]=a[i];p.step = k.step + 1;if(check(a,num,p.step))Q.push(p);a[i]=temp;}}}
}int main()
{int t;scanf("%d",&t);int c[7];memset(dp7,-1,sizeof(dp7));memset(dp6,-1,sizeof(dp6));memset(dp5,-1,sizeof(dp5));memset(dp4,-1,sizeof(dp4));memset(dp3,-1,sizeof(dp3));memset(dp2,-1,sizeof(dp2));memset(dp1,-1,sizeof(dp1));for(int i=0;i<7;i++)c[i]=i;for(int i=0;i<7;i++)solve(c,0,i+1);while(t--){H.clear();vector<int> Q;int n;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&a[i]);Q.push_back(a[i]);}sort(Q.begin(),Q.end());for(int i=0;i<Q.size();i++)H[Q[i]]=i;for(int i=0;i<n;i++)b[H[a[i]]]=i;if(n==1){if(b[0]==0)printf("0\n");elseprintf("%d\n",dp1[b[0]]);}if(n==2){if(b[0]==0&&b[1]==1)printf("0\n");elseprintf("%d\n",dp2[b[0]][b[1]]);}if(n==3){if(b[0]==0&&b[1]==1&&b[2]==2)printf("0\n");elseprintf("%d\n",dp3[b[0]][b[1]][b[2]]);}if(n==4){if(b[0]==0&&b[1]==1&&b[2]==2&&b[3]==3)printf("0\n");elseprintf("%d\n",dp4[b[0]][b[1]][b[2]][b[3]]);}if(n==5){if(b[0]==0&&b[1]==1&&b[2]==2&&b[3]==3&&b[4]==4)printf("0\n");elseprintf("%d\n",dp5[b[0]][b[1]][b[2]][b[3]][b[4]]);}if(n==6){if(b[0]==0&&b[1]==1&&b[2]==2&&b[3]==3&&b[4]==4&&b[5]==5)printf("0\n");elseprintf("%d\n",dp6[b[0]][b[1]][b[2]][b[3]][b[4]][b[5]]);}if(n==7){if(b[0]==0&&b[1]==1&&b[2]==2&&b[3]==3&&b[4]==4&&b[5]==5&&b[6]==6)printf("0\n");elseprintf("%d\n",dp7[b[0]][b[1]][b[2]][b[3]][b[4]][b[5]][b[6]]);}}
}

2015北京网络赛 G题 Boxes bfs相关推荐

  1. hihoCoder1233(2015北京网络赛H题)

    转载自:http://blog.csdn.net/queuelovestack/article/details/48625899 题意: 有n个卡槽,放有体积不同的n个空盒子,每次你可以移动一个空盒子 ...

  2. hihoCoder1228(2015北京网络赛B题)

    题意: 给出一个文本编辑器的容量,给出老板输入的字符串,小写字母代表文本,大写字母代表命令: L:光标左移: R:光标右移: S:在insert模式和另一个输入模式中切换: D:删除光标后面的一个字符 ...

  3. hdu5443(2015长春网络赛G题)

    题意: 一个数列,求从L到R的最大值. 思路: 不多说... 代码: #include<cstdio> #include<cstring>int a[20000];int ma ...

  4. hihoCoder 1227 2015 北京网络赛 A题

    题意: 给定m个点,然后从这m个点里找到一个点作为圆心,求一个最小的半径使得圆内刚好有n个点,没有压线的点. 思路: 预处理一下各点间的距离,暴力枚举圆心,然后找到排序后的第n个判断即可.坑点:n&g ...

  5. hdu5489(2015合肥网络赛F题)

    转载自:http://blog.csdn.net/lwt36/article/details/48774103 题意: 给出一个数列,在其中删除连续的L个数字,使得剩余的数字LIS最大,输出此LIS. ...

  6. hdu5491(2015合肥网络赛H题)

    题意: 给出三个数字D.s1和s2,用L来表示D的二进制表示中1的个数,L在区间[s1,s2]中,我们要找到离D最近的并且大于D的一个数字,且这个数字的L也落在区间[s1,s2]中. 思路: 一直超时 ...

  7. hdu5492(2015合肥网络赛I题)

    题意: n*m的格子,每个格子有权值,我们要从左上角走到右下角,只能向下走或者向右走,求走到终点走过的格子的方差的最小值. 思路: 被这题坑了,我还是太蠢. 我们可以暴力(∑Ai)^2,取最优就好了. ...

  8. hdu5455(2015沈阳网络赛F题)

    题意: 给出一个串,问用题中定义的那些串来组成这个串最少要用多少个. 思路: 没啥说的,注意一下输入的串中可能出现除了c和f的字母. 代码: #include<cstdio> #inclu ...

  9. hdu5442(2015长春网络赛F题)

    题意: 给出一个字符串,只由'a'~'z'组成,字符串是一个首尾相接的串.我们要找到一个起点,顺时针或者逆时针的读这个串,找到字典序最大的读法,如果有多种,输出起点坐标小的那个,如果起点坐标一样,输出 ...

最新文章

  1. 面试官:说说Java中的信号量?Semaphore
  2. jvm:运行时数据区--操作数栈
  3. tkinter 笔记 checkbutton 勾选项 (莫烦python笔记)
  4. 【渝粤教育】国家开放大学2018年秋季 0734-22T出纳实务 参考试题
  5. 前端学习(2891):vue工程化配置
  6. java委_java双亲委派机制及作用
  7. Python3中如何做的自定义模块的引用?
  8. 你可以有喜欢和善用的语言,但千万不要和她Bind
  9. (第39册)《微信小程序游戏开发快速入门到实战》夏敏捷著
  10. Python小白的数学建模课-19.网络流优化问题
  11. Linux配置ntp时间同步
  12. 二线制和四线制传感器的区别_两线制、三线制、四线制传感器的区别和原理
  13. HTTPSConnectionPool(host=‘finance.yahoo.com‘, port=443解决方案
  14. 【CSS3动画】利用CSS3制作“百度浏览器”官网奔跑的北极熊效果(不含背景移动)
  15. OKR 结果思维:为什么要以结果为导向?(第一部分)
  16. 同指数幂相减公式_同底指数加减运算法则
  17. html 让其中一个div浮在另一个div上面
  18. java快速对接微信支付分(一)
  19. 服务器pe安装win7系统安装教程,U盘PE安装Win7安装版|U盘安装win7原版教程
  20. pycharm更改黑色主题

热门文章

  1. NFS运维二班--梁瑞
  2. echarts 选中bush中lineX
  3. jquery-11 如何制作鼠标右键菜单
  4. display:table的几个用法
  5. .NET 4.5 Task异步编程学习资料
  6. c++ 门面模式(Facade)
  7. 串口项目——Cseiralport类的应用(1 )
  8. PHP中数组的三种排序方法
  9. HDU 2574 HDOJ 2574 Hdu Girls' Day ACM 2574 IN HDU
  10. 一种新的图像清晰度评价函数,数字图像清晰度评价函数的研究与改进