poj 3460 Booksort ida*

Booksort

Time Limit: 15000MS Memory Limit: 65536K
Total Submissions: 2722 Accepted: 1230

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本乱序的书,每次操作可以将连续的x本书插入到另一个位置,问至少需要几次操作才能恢复顺序。

每次操作最多可以改变3本书的后继,以此当作估价函数。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 20;
int num[N], n;

void modify(int l, int r, int ob) {
    int tmp[N], x = 1;
    for (int i = 1; i < l; i++) tmp[x++] = num[i];
    for (int i = r + 1; i <= ob; i++) tmp[x++] = num[i];
    for (int i = l; i <= r; i++) tmp[x++] = num[i];
    for (int i = ob + 1; i <= n; i++) tmp[x++] = num[i];
    for (int i = 1; i <= n; i++) num[i] = tmp[i];
}

int dfs(int now, int dep) {
    int cnt = 0;
    for (int i = 1; i < n; i++) 
        if (num[i + 1] != num[i] + 1) cnt ++;
    if (num[n] != n) cnt ++;

    if (cnt == 0) return 1;
    if (3 * (dep - now) < cnt) return 0;
    
    int tmp[N];
    for (int i = 1; i <= n; i++) tmp[i] = num[i];
    
    for (int l = 1; l <= n; l++) 
        for (int r = l; r <= n; r++) 
            for (int ob = r + 1; ob <= n; ob++) {
                modify(l, r, ob);
                if (dfs(now + 1, dep)) return 1;
                for (int j = 1; j <= n; j++) num[j] = tmp[j];
            }
    return 0;
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        int flag = 0;
        for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
        for (int i = 0; i < 5; i++) {
            if (dfs(0, i)) {
                flag = 1;
                printf("%d\n", i);
                break;
            }
        }
        if (!flag) printf("5 or more\n");
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值