[POJ3460] Booksort 题解

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

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的数,每次可以将任意两段不重复的区间交换顺序,问至少交换几次能实现1~n的顺序(若大于等于5次则直接输出5 or more).

思路:看到5 or more我们可以很容易的想到迭代搜索.当某次操作后该序列为递增序列时就找到了最小操作次数.我们很容易想到在判断是否顺序时能够找到'当前可能的最小操作次数'.即:每次操作都可以更改掉3个非顺序的数字(由于每次更改只和前后有关故单次操作最多更改数为3)时还需要的操作次数.我们可以使用这个值进行一个对当前操作的估计:若当前已使用的操作数加上还需最小操作数大于了当前所限制迭代深度,我们就可以省去当前分支.


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define rg register
#define LL long long
#define __space putchar(' ')
#define __endl putchar('\n')
template <typename qwq> inline void read(qwq & x)
{
    x = 0;
    rg int f = 1;
    rg char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    x *= f;
}
template <typename qaq> inline void print(qaq x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
int t,n,id;
int a[233];
inline void move(int x,int y,int z)
{
    //交换[x,y] [y + 1,z]的位置; 
    int temp[233],cnt = 0;
    for (rg int i = y + 1;i <= z;++i) temp[++cnt] = a[i];
    for (rg int i = x;i <= y;++i) temp[++cnt] = a[i];
    cnt = 0;
    for (rg int i = x;i <= z;++i) a[i] = temp[++cnt]; 
}
inline bool IDA_star(int d)
{
    rg int tot = 0;
    for (rg int i = 1;i < n;++i)//枚举搬书的起点 
    {
        for (rg int j = i;j < n;++j)//枚举搬书的终点,j - i + 1即为搬的本数 
        {   
            for (rg int k = j + 1;k <= n;++k)//枚举和哪些书换位置 
            {
                move(i,j,k);//将[i,j]与到[j + 1,k]换位 
                tot = 0;
                //=====================================
                for (rg int l = 1;l < n;++l)
                {
                    if (a[l + 1] != a[l] + 1) ++tot;
                }
                if (a[n] != n) ++tot;
                if (!tot) return true;
                //=====================================
//              rg int f = ceil(tot * 1.0 / 3);
//              if (d + f > id) return false;
                if (3 * d + tot <= 3 * id)
                {
                    if (IDA_star(d + 1)) return true;
                }
                move(i,i - j + k - 1,k);//回溯 
            }
        }
    }
    return false;
}
int main()
{
    read(t);
    while (t--)
    {
        read(n);
        id = 0;
        for (rg int i = 1;i <= n;++i) read(a[i]);
        for (rg int i = 1;i < n;++i)
        {
            if (a[i + 1] != a[i] + 1) ++id;
        }
        if (a[n] != n) ++id;
        if (!id)
        {
            print(0),__endl;
            continue;
        }
        id = ceil(id * 1.0 / 3);
        while (id <= 4)
        {
            if (IDA_star(1))
            {
                print(id),__endl;
                break;
            }
            ++id;
        }
        if (id > 4) puts("5 or more");  
    }
    return 0;
}
全部评论

相关推荐

05-29 19:11
已编辑
北方民族大学 Java
😭😭😭😭本人26届双非本,后端选手。从25年秋招开始,一直到春招5月份,一共面了12次字节。可以说后面能继续投递面上字节大概率是因为前面一直累计的面评还不错,但是最终的结果往往不尽如人意,黄梁一梦。timeline:如标题,总共面了12次字节,4个不同的岗位。第一次:抖音生活服务测开二面完排序挂第二次:TikTok国际化电商测开三面完排序挂第三次:飞书后端安全团队三面完挂第四次:飞书后端偏基架团队三面完过,HR面完之后询问综合排序不推进。我知道像BAT这样的公司,双非本想拿到一张入场券有多难,也知道每次挂在排序/三面/HR面,那种差一步上岸又被打回原点的落差感有多磨人。可是最后一次字节的这个岗位,已经是5月中旬才开始面得了,春招末期的岗位,我本以为真的缺人,三面过的那天,我真的以为就差一步hr面就稳了,但是,最终的结果很遗憾,综合排序综合排序,不推进了。如果是技术能力的问题,我想也不会每一轮技术面给我通过。思来想去。难道真的就是因为我们双非有案底,所以最后的一切又算什么呢。付出这么多的时间精力,还是抵不过双非学历太差吗?既然如此一开始直接卡掉简历不用给面试不就行了嘛,每一轮面试都给我们生的希望,最后的最后又回到了那个必输的起点。12次字节,说不遗憾是假的,也无数次怀疑过自己:是不是我算法刷得还不够?是不是项目亮点讲得不够好?是不是学历就是一道跨不过去的坎?但回头看,这一年的秋招到春招,从面对面试官紧张到说话卡壳,到后来的从容面对,再到如今甚至能和面试官探讨AI&amp;大模型技术的一些方案思路,我已经比去年的自己强太多了。可能字节于我,真的是一场盛大的单恋,拼尽全力奔赴,却还是没能收到想要的回应。前路漫漫,字节的梦碎了,但我的路还在继续,希望下一站,会有属于我的一场徐风。
不愿吃饼的山羊很友好:你的心理素质是真的强大,如果是我碰到这样都会疯了
点赞 评论 收藏
分享
04-24 18:13
南京大学 Java
不吃酸菜血肠:看力竭了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务