POJ 3460Booksort
来源:互联网 发布:用什么相机拍淘宝衣服 编辑:程序博客网 时间:2024/06/07 05:11
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.
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
361 3 4 6 2 555 4 3 2 1106 8 5 3 4 7 2 9 1 10
Sample Output
235 or more
IDA*算法
因为每次移动最多改变三个后继值
估计函数f(x)=当前后继数不等于当前数+1的个数/3
#include<cstdio>#include<iostream>#include<cstring>#include<map>#include<queue>#include<stack>#include<algorithm>#include<vector>#include<cmath>#include<string>#include<functional>using namespace std;const int maxn=25;int T,n;bool flag;struct state{int a[maxn];int now(){int ans=0;for (int i=0;i<n-1;i++)if (a[i]+1!=a[i+1]) ans++;return ans/3;}bool check(){for (int i=0;i<n-1;i++)if (a[i]+1!=a[i+1]) return false;return true;}state move(int l,int r,int len){state u=*this;for (int i=l+len;i<=r;i++) u.a[i-len]=u.a[i];for (int i=r-len+1;i<=r;i++) u.a[i]=this->a[i+l-r+len-1];return u;}}bg;void dfs(state x,int y,int limit){if (x.now()+y>limit||flag) return;if (x.check()){flag=true; return;}for (int len=1;len<n;len++)for (int i=0;i+len-1<n;i++)for (int j=i+len;j<n;j++)dfs(x.move(i,j,len),y+1,limit);}int main(){cin>>T;while (T--){cin>>n;for (int i=0;i<n;i++) scanf("%d",&bg.a[i]);for (int i=0;i<5;i++){flag=false;dfs(bg,0,i);if (flag) {printf("%d\n",i); break;}}if (!flag) printf("5 or more\n");}return 0;}
- POJ 3460 Booksort IDA*
- poj 3460 Booksort(IDA*)
- POJ 3460 Booksort IDA*
- POJ 3460Booksort
- poj 3460 Booksort
- POJ 3460 Booksort (IDA*)
- hdu 1685 Booksort (IDA*)
- HDU-1685-Booksort(IDA*)
- hdu 1685 Booksort (搜索)
- 解题报告:POJ_3460&HDU_1685 Booksort IDA*
- POJ 3460 IDA*
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- ios开发遇到的问题
- Java基础
- 1014. Waiting in Line (30)
- 51nod 1091 线段的重叠
- Android开发相关的Blog推荐——跟随大神的脚步才能成长为大神
- POJ 3460Booksort
- SQL连接
- ubuntu_maven
- 【CSS】——基础认识
- JAVA_SE基础——15.循环嵌套
- java中初始化对象变量的方法
- java中String s="abc"及String s=new String("abc")详解
- POJ 2977Box walking
- running android lint has encountered a