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[]]);
} }
}