Booksort POJ - 3460 (IDA*)

Description

The Leiden University Library has millions of books. When a student wants to borrow a certain book, he usually submits an online loan form. If the book is available, then the next day the student can go and get it at the loan counter. This is the modern way of borrowing books at the library.

There is one department in the library, full of bookcases, where still the old way of borrowing is in use. Students can simply walk around there, pick out the books they like and, after registration, take them home for at most three weeks.

Quite often, however, it happens that a student takes a book from the shelf, takes a closer look at it, decides that he does not want to read it, and puts it back. Unfortunately, not all students are very careful with this last step. Although each book has a unique identification code, by which the books are sorted in the bookcase, some students put back the books they have considered at the wrong place. They do put it back onto the right shelf. However, not at the right position on the shelf.

Other students use the unique identification code (which they can find in an online catalogue) to find the books they want to borrow. For them, it is important that the books are really sorted on this code. Also for the librarian, it is important that the books are sorted. It makes it much easier to check if perhaps some books are stolen: not borrowed, but yet missing.

Therefore, every week, the librarian makes a round through the department and sorts the books on every shelf. Sorting one shelf is doable, but still quite some work. The librarian has considered several algorithms for it, and decided that the easiest way for him to sort the books on a shelf, is by sorting by transpositions: as long as the books are not sorted,

  1. take out a block of books (a number of books standing next to each other),
  2. shift another block of books from the left or the right of the resulting ‘hole’, into this hole,
  3. and put back the first block of books into the hole left open by the second block.

One such sequence of steps is called a transposition.

The following picture may clarify the steps of the algorithm, where X denotes the first block of books, and Y denotes the second block.

Original situation:
After step 1:
After step 2:
After step 3:

Of course, the librarian wants to minimize the work he has to do. That is, for every bookshelf, he wants to minimize the number of transpositions he must carry out to sort the books. In particular, he wants to know if the books on the shelf can be sorted by at most 4 transpositions. Can you tell him?

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with one integer n with 1 ≤ n ≤ 15: the number of books on a certain shelf.
  • One line with the n integers 1, 2, …, n in some order, separated by single spaces: the unique identification codes of the n books in their current order on the shelf.

Output

For every test case in the input file, the output should contain a single line, containing:

  • if the minimal number of transpositions to sort the books on their unique identification codes (in increasing order) is T ≤ 4, then this minimal number T;
  • if at least 5 transpositions are needed to sort the books, then the message "5 or more".

Sample Input

3
6
1 3 4 6 2 5
5
5 4 3 2 1
10
6 8 5 3 4 7 2 9 1 10

Sample Output

2
3
5 or more

题意:给n本书,每次可以选取其中一段移动到另外的位置,使得书的编号变成1~n,如果步数超过4,就输出5 or more,反则输出步数。

思路:抽取书同一长度的书,有n-len+1种选择,有n-len个位置可以插入,并且因为一位置len长度后移等于后面位置前移

Σ(n-len)*(n-len+1)/2 <= (15*14+14*13+...+2*1)/2 == 560 ,因为只用判断是否步数<=4,不同搜索(560)4,时间复杂度过大。

通过IDA*(迭代加深+A*)

迭代加深:因为我们确定答案会在5之前收到

A*:评估函数,因为一次移动,最多可以使得3本书后面的书籍编号改变,所以该状态到达终态的至少步数f = ⌈错误后继/3⌉

(红色为三本书,是一次移动种最多改变的三本,橙色区域代表移动的区域,移动至第一个红色书的后面,那么第二个和第三个红色书的后继都改变了)

#include<iostream>
#include<cstdio>
#include<cmath>

using namespace std;

const int maxn = 22;
int t;
int n;

struct Node
{
    int s[maxn];
};

bool check(int s[])
{
    for(int i=1; i<n; i++)
        if(s[i] + 1 != s[i+1])
            return 0;
    return 1;
}

Node change(Node x,int l,int r,int len)
{
    Node tmp = x;
    for(int i=l; i<l+len; i++)
        x.s[r-len-l+i+1] = tmp.s[i];
    for(int i=l+len; i<=r; i++)
        x.s[i-len] = tmp.s[i];
    return x;
}

int cal(int s[])
{
    int ans = 0;
    for(int i=1; i<n; i++)
        if(s[i] + 1 != s[i+1])
            ans++;
    return ceil(ans/3.0);
}

bool dfs(Node x,int now,int lim)
{
    if(now + cal(x.s) > lim)
        return 0;
    if(check(x.s))return 1;
    for(int len=1; len<n; len++)
    {
        for(int i=1; i+len-1 <=n; i++)
        {
            for(int j=i+len; j<=n; j++)
            {
                if(dfs(change(x,i,j,len),now+1,lim))return 1;
            }
        }
    }
    return 0;
}

int main()
{
    scanf("%d",&t);
    Node start;
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
            scanf("%d",&start.s[i]);
        for(int i=0; i<5; i++)
        {
            if(dfs(start,0,i))
            {
                printf("%d\n",i);
                break;
            }
            else if(i == 4)printf("5 or more\n");

        }
    }
}

原文地址:https://www.cnblogs.com/iwannabe/p/10624745.html

时间: 2024-11-10 10:06:47

Booksort POJ - 3460 (IDA*)的相关文章

POJ 2253-Frogger (Prim)

题目链接:Frogger 题意:两只青蛙,A和B,A想到B哪里去,但是A得弹跳有限制,所以不能直接到B,但是有其他的石头作为过渡点,可以通过他们到达B,问A到B的所有路径中,它弹跳最大的跨度的最小值 PS:最小生成树过的,刚开始用Dijstra做,Kao,精度损失的厉害,对于Dijksra的变形不大会变啊,看了Discuss有人用最小生成树过,我一划拉,还真是,敲了,就过了,等会研究研究最短路的各种变形,有模板不会变,不会灵活应用,渣渣就是渣渣. ME               Time 10

poj 1861(最小生成树)

Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access

POJ 2352 (stars)

[题意描述] 就是给定n个星星的x,y坐标,y坐标按照从小到大的顺序进行排列,x坐标随机排列.下面求对于每个星星而言,其它星星的x,y的坐标都小于等于该星星的数目,然后输出所有的情况. [思路分析] 我们这道题可以采用树状数组求解,将x+1作为树状数组的底标. [AC代码] #include<iostream> #include<bitset> #include<cstdio> #include<cstring> #include<algorithm&

poj Sudoku(数独) DFS

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13665   Accepted: 6767   Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure.

POJ 3419 (rmq)

这道题是rmq,再加上一个解决溢出. 刚开始我也想过用rmq,虽然不知道它叫什么,但是我知道应该这样做.可是后来没想到这道题的特殊性,也就是解决溢出的方法,就放弃了. rmq可以用线段树,也可以用dp.  这道题都可以过的,而且线段树要快一些. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; #define m

POJ题目(转)

http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (

poj数论(转)

1.burnside定理,polya计数法    这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能完全看懂了,理解了再去做题,不要只记个公式.    *简单题:(直接用套公式就可以了)    pku2409 Let it Bead       pku2154 Color    pku1286 Necklace of Beads    *强烈推荐:(这题很不错哦,很巧妙)    pku2888 Magic Bracelet2.置换,置换的运算 置换的概念

poj 1200 (hash)

Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23168   Accepted: 6513 Description Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden prime number in a gi

POJ - 2286 - The Rotation Game (IDA*)

IDA*算法,即迭代加深的A*算法,实际上就是迭代加深+DFS+估价函数 题目传送:The Rotation Game AC代码: #include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <queue> #include <stack> #include <bitset> #include