解题报告:POJ_3460&HDU_1685 Booksort IDA*

来源:互联网 发布:淘宝网支架白板 编辑:程序博客网 时间:2024/04/29 02:19

Booksort

Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 377    Accepted Submission(s): 196


Problem 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,

take out a block of books (a number of books standing next to each other),
shift another block of books from the left or the right of the resulting ‘hole’, into this hole,
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.



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
361 3 4 6 2 555 4 3 2 110 6 8 5 3 4 7 2 9 1 10
 

Sample Output
235 or more
 

Source
华东区大学生程序设计邀请赛_热身赛
 

Recommend
lcy
 

Statistic | Submit | Discuss | Note

题意:
给定一个元素为1到n的无序数列,问最少多少次操作可以将它变成正常升序,每次操作可以截取一串连续子序列再插入数列的任意处

思路:
IDA*,迭代加深操作数,当3*(已操作次数)+无序个数>3*总操作数时剪枝,无序数为A[i]+1!=A[i+1],因为每次操作最多使三个数的序变得正确(1.截取处位置的序。2.插入位置的前序。3.插入位置的后序)。

代码:

#include<stdio.h>int n;int ans ;int str[20];inline int h(int*s){    int res = 0;    for(int i=1;i<n;i++){        if(s[i]+1!=s[i+1])            res ++;    }return res ;}inline bool check(int *str){    for(int i=1;i<=n;i++){        if(i!=str[i]){            return false ;        }    }return true;}inline void work(int startid,int lenth,int to,int*p,int*s){    int j=1,id=startid,l=lenth;    for(int i=1;i<=n;i++){        if(j==startid){            j+=l;        }        if(i==to){            while(lenth--){                s[i++]=p[id++];            }i--;        }else {            s[i]=p[j++];        }    }}bool dfs(int step,int*s,int lim){    int p[20];    if(3*step-3+h(s)>3*lim)return false ;    for(int lenth = 1;lenth<=n/2;lenth++){        for(int startid = 1;startid<=n-lenth+1;startid++){            for(int to = 1;to<=n-lenth+1;to++){                if(to==startid)continue;                work(startid,lenth,to,s,p);                if(step==lim&&check(p)){                    return true;                }else if(step!=lim){                    if(dfs(step+1,p,lim)){                        return true;                    }                }            }        }    }    return false ;}int main(){    int T;    scanf("%d",&T);    while(T--){        scanf("%d",&n);        for(int i=1;i<=n;i++){            scanf("%d",&str[i]);        }        ans = check(str)?0:5;        for(int step=1;step<5&&ans;step++){            if(dfs(1,str,step)){                ans = step;                break;            }        }        if(ans>=5){            printf("5 or more\n");        }else {            printf("%d\n",ans);        }    }return 0;}


0 0