ACM Learning process-hihocoder 1233 Boxes (BFS) (2015 Beijing online game)

Source: Internet
Author: User

Hihocoder Challenge 12

Time limit: 1000ms

Single point time limit: 1000ms

Memory Limit: 256MB

Describe

There is a strange storehouse in PKU. In this storehouse there is n slots for boxes, forming a line. In each slot, you can pile up to any amount of boxes. The limitation is so you can have only pile a smaller one above a bigger one and in order to keep balance. The slots is numbered from 1 to N. The leftmost one is slot 1.

At first there are exactly one box in each slot. The volume of the box in slots I is VI. As a Virgo, you decide to sort these boxes by moving some of them. In each move can choose a slots and move the top box in this slot to an adjacent slot (for course you can only put it on The top). You should ensure the limitation mentioned above are still satisfied after this move. After the sort operation, there should is exactly one box in each slot, and for each pair of adjacent slots, the box in th E left one should is 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, all VI in a test case, is 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

The title looks like Hanoi, but the conditions are even more harsh.

However, due to n < 8, the maximum number of states is 1!+2!+3!+ ... 7!.

However, if you want to simulate the moving process, the box will overlap during the move, so the number of States should be 1^1+2^2+3^3 ... 7^7.

The number of this state is not particularly large.

And the value of this state for 1,12,123,1234,12345,123456,1234567 is 0.

So, you can start from these 0 states, to simulate the process of moving the box, and then the BFS to obtain all the solution of the state. Equivalent to a backward process.

First of all, the number must be processed into successive numbers, such as 123456. There are many ways to deal with this.

But the storage of state is a problem.

Can be stored with a struct, then there will be an array of length 7 index[i], which represents the location of the box with size I. This requires a map to save the results, but it needs to reload the less-than sign.

A string can be used without overloading the less-than number, but you also need a map to save the results.

But the efficiency of the map here is Logn, the test time is very long.

In other words, this logn can not be multiplied, although in theory the computational complexity should be enough, but in fact 1S can not be processed.

It is also said that the mapping relationship between the state and the result needs to be done within constant time.

Since the maximum state is 7777777, that is, it is possible to use a single-dimensional array to store the state directly with a decimal int.

The web is also useful for binary pressure, each three bits of the position of a box, so that up to 21 bits can represent all the state of the box, that is, 2097152.

Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<map>#include<queue>#include<string>#defineLL Long Longusing namespacestd;structarg{intVal; intID;} arg[Ten];BOOLCmparg (Arg x, arg y) {returnX.val <Y.val;}intN;ints[Ten];intans[8000000];voidBFsintCNT) {    intk[Ten], TMP, State, MI; Queue<int>Q; State=0;  for(inti =1; I <= CNT; ++i) state=Ten*state+i; Ans[state]=0;    Q.push (state);  while(!Q.empty ()) { state=Q.front ();        Q.pop (); TMP=State ;  for(inti =0; I < CNT; ++i) {k[cnt-1-I.] = tmp%Ten; TMP/=Ten; }         for(inti =0; I < CNT; ++i) {mi= -1;  for(intj =0; J < I; ++j) {if(K[j] = =K[i]) {mi=J;  Break; }            }            if(Mi! =-1)                Continue; if(K[i] >1) {K[i]--; Mi= -1;  for(intj =0; J < I; ++j) {if(K[j] = =K[i]) {mi=J;  Break; }} tmp=0;  for(inti =0; I < CNT; ++i) TMP=Ten*tmp+K[i]; if(Ans[tmp] = =-1&& mi = =-1) {ans[tmp]= ans[state]+1;                Q.push (TMP); } K[i]++; }            if(K[i] <CNT) {K[i]++; Mi= -1;  for(intj =0; J < I; ++j) {if(K[j] = =K[i]) {mi=J;  Break; }} tmp=0;  for(inti =0; I < CNT; ++i) TMP=Ten*tmp+K[i]; if(Ans[tmp] = =-1&& mi = =-1) {ans[tmp]= ans[state]+1;                Q.push (TMP); } K[i]--; }        }    }}voidinit () {memset (ans,-1,sizeof(ans));  for(inti =1; I <8; ++i) BFS (i);}voidinput () {scanf ("%d", &N);  for(inti =0; I < n; ++i) {scanf ("%d", &arg[i].val); Arg[i].id=i; } sort (ARG, arg+N, Cmparg);  for(inti =0; I < n; ++i) s[i]=arg[i].id+1;}voidWork () {if(n = =0)        return; intK =0;  for(inti =0; I < n; ++i) K=Ten*k+S[i]; printf ("%d\n", Ans[k]);}intMain () {//freopen ("test.in", "R", stdin);init (); intT; scanf ("%d", &T);  for(inti =0; i < T; ++i) {input ();    Work (); }    return 0;}

ACM Learning process-hihocoder 1233 Boxes (BFS) (2015 Beijing online game)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.