Booksort HDU - 1685

题目链接:Booksort HDU - 1685

===================================================

Booksort

Time Limit: 10000/10000 MS (Java/Others)
Memory Limit: 102400/102400 K (Java/Others)

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

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

===================================================

题意:

  • 给你一个打乱顺序的序列,每次操作可以选择一个长度的 子连续序列 插入任意位置,问最少需要多少次操作变成升序序列。
  • 条件就是序列的数字一定连续的数字,即 1,2,3,4,5这样的数字。然后错过4次操作的答案,一律为5 or more
  • 举例:5 4 3 2 1 ------> 3 2 5 4 1 -------> 3 4 1 2 5 ------------> 1 2
    3 4 5 只要3步。

算法:IDA* /dfs

思路1:

  • 题意明确只要4步以内就行了。那就使用 IDA*的dfs进行limit+1慢慢推进,直到得出答案。

  • 难点:启发函数的设定。

  • 序列分块,连续的为一块,即1 4 2 3 5 序列 分成 4块 1、 4 、 23、 5;

  • 这里注意, 如果第一块为1,其本身不应该算进去,因为至始至终都不会移动这一块;最末端块同理;

  • 因为 1 和 5本身就在其最终位置上永远不需要动,所以 只要改变 4,23两块就行了,即需要操作的块数为2。

  • 然后每次操作都会影响3块的后缀块变化,即分离块的前一块的、分离块本身与被插入点前面那块这3块的后缀;理想情况下,每次改变都使得其连接成一块,即每次操作减少3块。

  • 所以1 4 2 3 5 序列 启发函数 h(x) = ceil(操作块数/3),向上取整,即是1。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

const int INF = 2e9;

int n,num[20];

void init(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>num[i];
    num[0]=0;num[n+1]=n+1;
}

void show(){
    for(int i=1;i<n+1;i++){
        cout<<num[i]<<" ";
    }cout<<endl;
}

int forLimit(){
    double ans = 0;
    for(int i=0;i<=n;i++) if(num[i]+1!=num[i+1]) ans++;
    return ceil(ans/3);
}

bool dfs(int step,int limit){
    int h = forLimit();
    if(h+step>limit) return false;
    if(h==0){
        cout<<step<<endl;
        //show();
        return true;
    }
    int temp[20];
    memcpy(temp,num,sizeof(num));
    for(int i=1;i<=n;i++) for(int len=1;len<=n-i+1;len++) for(int pos=1;pos<=n-len+1;pos++) if(pos!=i){
        for(int j=0;j<len;j++) num[pos+j] = temp[i+j];
        for(int p=1,j=1;j<=n;){
            if(j<pos) num[j++] = temp[p++];
            else if(j==pos) j+=len;
            else if(p==i) p+=len;
            else num[j++] = temp[p++];
        }
        if(dfs(step+1,limit)) {
                //memcpy(num,temp,sizeof(temp));show();
                return true;
        }
    }
    memcpy(num,temp,sizeof(temp));
    return false;
}

int main()
{
    int _;cin>>_;
    while(_--){
        init();
        for(int limit=forLimit();;limit++){
            if(limit>4) {puts("5 or more");break;}
            if(dfs(0,limit)) break;
        }
    }
}

===================================================

思路2:

  • 观看大神博客https://blog.csdn.net/ilsswfr/article/details/52012467

类似于双向遍历bfs一样。

第一步:从初始序列开始dfs两步,得出答案为解。用set记录所有得出的序列。

第二部:从目标序列开始dfs两步,如果出现的序列在set里面则步数+2为解。

//从初始情况搜索两层并记录每次状态,这两层中如果有解,直接输出
//若是无解,从有序状态往回搜一层和两层,
//如果能达到与之前的相同的状态就是有解。
 
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#define pi acos(-1.0)
#define inf 1<<29
#define INF 0x3f3f3f3f
#define zero 1e-8
 
using namespace std;
int step, n;
int arr[100];
 
int fin[100];
 
set<string> ff;
 
int makenum(int num, char *s, int &k)
{
 
    int i = 0, now[5] = {};
    for (; num;) {
        now[++i] = num % 10;
        num /= 10;
    }
    for (int j = i; j; j--) {
        s[k++] = now[j] + '0';
    }
    return k;
 
}
 
string makestr(int *s, int num)  //样式
{
 
    string aa;
    char a[100];
    int k = 0;
    for (int i = 0; i < num; ++i) {
        makenum(s[i], a, k);
        a[k++] = ' ';
    }
    a[k] = '\0';
    return a;
 
}
bool compare(int *s)
{
    for (int i = 0; i < n; ++i)
        if (fin[i] != s[i]) return false;
 
    return true;
}
void show(int *s)
{
    for (int i = 0; i < n; ++i)
        cout << s[i];
    cout << endl;
}
void cop(int *en, int *be)
{
    for (int i = 0; i < n; ++i)
        en[i] = be[i];
}
void dfs1(int *tem, int cnt, int now)
{
    string str = makestr(tem, n);
    ff.insert(str);
    if (step != inf) return ;
    if (cnt == now) {
        if (compare(tem)) {
            step = cnt;
        }
 
        return ;
    }
 
    if (cnt < now) return ;
    int tt[100];
 
    for (int j = 0; j < n; ++j) {
 
        int z;
        for (z = 0; z < j; ++z)
            tt[z] = tem[z];
        for (int len = 1; len < n - j; ++len) {
            cop(tt, tem);
 
            int f = z + len;
            for (int sert = 0; sert < n - j - len; ++sert) {
                tt[z + sert] = tem[f + sert];
 
                if (f + sert + 1 < n - j - len
                        && tem[f + sert + 1] == tt[z + sert] + 1) continue;
 
                for (int kk = 1; kk <= len; ++kk)
                    tt[z + sert + kk] = tem[j + kk - 1];
 
                string str = makestr(tt, n);
                ff.insert(str);
                dfs1(tt, cnt, now + 1);
            }
 
        }
    }
}
 
void dfs2(int *tem, int cnt, int now)
{
 
    if (step != inf) return ;
    string str = makestr(tem, n);
    if (cnt == now) {
        if (ff.find(str) != ff.end())
            step = 0;
        return ;
    }
 
    int tt[100];
 
    for (int j = 0; j < n; ++j) {
 
        int z;
        for (z = 0; z < j; ++z)
            tt[z] = tem[z];
        for (int len = 1; len < n - j; ++len) {
            cop(tt, tem);
 
            int f = z + len;
            for (int sert = 0; sert < n - j - len; ++sert) {
                tt[z + sert] = tem[f + sert];
 
                if (f + sert + 1 < n - j - len
                        && tem[f + sert + 1] == tt[z + sert] + 1) continue;
 
                for (int kk = 1; kk <= len; ++kk)
                    tt[z + sert + kk] = tem[j + kk - 1];
                dfs2(tt, cnt, now + 1);
            }
 
        }
    }
}
 
void makefin()
{
    for (int i = 0; i < n; ++i)
        fin[i] = i + 1;
}
 
int main()
{
    int t;
    cin >> t;
    for (; t--;) {
        scanf("%d", &n);
 
        for (int i = 0; i < n; ++i) {
            scanf("%d", &arr[i]);
        }
        makefin();
        step = inf;
 
        for (int i = 0; i <= 2; ++i) {
            step = inf;
            ff.clear();
            dfs1(arr, i, 0);
            if (step != inf) {
                printf("%d\n", step);
                break;
            }
        }
        if (step == inf) {
            for (int i = 1; i <= 2; ++i) {
 
                dfs2(fin, i, 0);
                if (step != inf) {
                    printf("%d\n", i + 2);
                    break;
                }
            }
            if (step == inf) printf("5 or more\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盐太郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值