BestCoder Round #59 (div.2) 1002 Reorder the Books

本文介绍了一个图书排序问题,通过数学分析和算法设计,提出了一种高效解决图书顺序恢复的方法。问题来源于一套《SDOI故事集》的图书被打乱顺序,需要通过特定的操作方式将图书恢复到原本的顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

dxy家收藏了一套书,这套书叫《SDOI故事集》,《SDOI故事集》有n(n\leq 19)n(n19)本,每本书有一个编号,从11号到nn号。
dxy把这些书按编号从小到大,从上往下摞成一摞。dxy对这套书极其重视,不允许任何人动这套书。
有一天Evensgn到dxy家玩,dxy因为和妹子有约会,就让Evensgn自己待在他家。Evensgn对这套书非常好奇,偷偷的看了一下,结果发现这里面竟然有当年小E和小Q的故事。Evensgn看得出神,结果把一摞书的顺序打乱了。
眼看着dxy就要回来了,Evensgn需要尽快把这摞书恢复到原先排好序的状态。由于每本书都非常重,所以Evensgn能做的操作只有把一本书从书堆中抽出来,然后把这本书放到书堆的顶部。
给你打乱的书的顺序,你能帮Evensgn算算最少需要几次上述的操作,他才能把这套书恢复顺序?假如你能算出来的话,Evensgn答应送给你一本他签名的书《SDOI故事集9:小E的故事》
输入描述
输入包含多组数据。
第一行包含一个正整数T(T\leq 30)T(T30)表示数据组数。
对于每组数据,第一行为一个正整数nn表示这套《SDOI故事集》中有多少本书。
接下来一行nn个用空格分开的正整数,表示Evensgn打乱后的这摞书的书号顺序(从上往下)。
输出描述
对于每组数据,输出一行一个整数,表示Evensgn最少需要几次操作才能讲书恢复顺序。
输入样例
2
4
4 1 2 3
5
1 2 3 4 5
输出样例
3
0
Hint
对于第一组数据,我们先把33号书放到最上面,接着操作22号书,最后操作11号书,(4,1,2,3)\rightarrow (3,4,1,2)\rightarrow(2,3,4,1)\rightarrow(1,2,3,4)(4,1,2,3)(3,4,1,2)(2,3,4,1)(1,2,3,4),这样就有序了
对于第二组数据,这摞书本来就有序了,所以不需要任何操作
n>15n>15的数据不超过1010


Reorder the Books(官方题解)

By davidlee1999WTK

把这题的模型简化一下,有一个1\rightarrow n1n的排列形成的数列,我们要用最少的操作次数把这个数列排序,每次操作都是把一个数放到整个数列的最前面。 首先我们可以注意到每个数最多只会被操作一次。因为假如有一个数被往前拿了两次,显然第一次的操作是没有意义的。 然后能发现一定先操作大的数,再操作小的数。因为假如先把小的数放前面去了,再把大的数放前面去,小的数就又在大的数后面了,小的数必定还得再操作一次,然而操作两次是不划算的。

到这里,对于19的数据范围,我们有一个很暴力的做法,2^n2n枚举要操作哪些数,这些操作按数的大小从大往小的顺序,模拟一下,然后检查一下最后的序列是否有序,复杂度O(n*2^n)O(n2n)

我们很快又能发现,假如操作了大小等于kk的数,那么所有小于kk的数也都得操作了。所以我们不用2^n枚举,直接mm11开始从小到大枚举,表示要操作前mm小的数,然后模拟,验证,这样复杂度为O(n^2)O(n2)

不过其实mm也是不用枚举的。 首先可以发现最大的数nn是不用操作的(其他数操作好了,数"nn"自然就在最后面了)。 于是我们先找到最大的数"nn"的位置,从这个位置往前找,直到找到(n-1)(n1)。假如找到头也没找到(n-1)(n1),那么数"(n-1)(n1)"需要操作,而一旦操作了(n-1)(n1),根据前面结论,总共就需要(n-1)(n1)次操作了;假如找到了(n-1),那么数"(n-1)(n1)"也不需要操作(和数"nn"不需要操作一个道理)。 同理,我们接着从(n-1)(n1)的位置往前找(n-2)(n2),再从(n-2)(n2)的位置往前找(n-3)(n3)...假如数kk找不到了,那么就至少需要kk次操作。这种做法的复杂度是O(n)O(n)




实现代码如下:

#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
    int t,n,book[20];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
          scanf("%d",&book[i]);
        int cnt=n-1,ans=n;
        while(cnt>=0)
        {
            if(book[cnt]==ans) ans--;
            cnt--;
        }
        printf("%d\n",ans);
    }
    return 0;
}



Sure, here&#39;s the Python code to read the student information from "stScore.txt", calculate the average score for each student, reorder the information based on the average score, and save the results in a new file named "stNew.txt": ```python # open stScore.txt file for reading with open("stScore.txt", "r") as f: lines = f.readlines() # create a dictionary to store student information students = {} # loop through each line in the file for line in lines: # split the line into parts parts = line.strip().split() # get the student ID, name, and scores sid, name, scores = parts[0], parts[1], parts[2:] # convert scores to integers scores = [int(s) for s in scores] # calculate the average score avg_score = sum(scores) / len(scores) # add the student information to the dictionary students[sid] = {"name": name, "scores": scores, "avg_score": avg_score} # sort the students by average score in descending order sorted_students = sorted(students.items(), key=lambda x: x[1]["avg_score"], reverse=True) # open stNew.txt file for writing with open("stNew.txt", "w") as f: # loop through the sorted students for student in sorted_students: sid, data = student name, scores, avg_score = data["name"], data["scores"], data["avg_score"] # write the student information to the file f.write(f"{sid} {name} {&#39; &#39;.join(map(str, scores))} {avg_score:.2f}\n") print("Done.") ``` Here&#39;s an example of the input file "stScore.txt": ``` 1001 Tom 80 90 85 1002 Jack 78 80 82 1003 Alice 90 95 92 1004 Bob 70 75 80 ``` And here&#39;s an example of the output file "stNew.txt" after running the above code: ``` 1003 Alice 90 95 92 92.33 1001 Tom 80 90 85 85.00 1002 Jack 78 80 82 80.00 1004 Bob 70 75 80 75.00 ``` As you can see, the students are reordered based on their average score in descending order, and the average score is added at the end of each line with two decimal places.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值