[Python](PAT)1025 PAT Ranking(25 分)

版权声明:首发于 www.amoshuang.com https://blog.csdn.net/qq_35499060/article/details/82120577

Python写PAT甲级,答案都在这儿了

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

题目大意

给定一次PAT测试的成绩,要求输出考生的编号,总排名,考场编号以及考场排名。

分析

每一次接收一个考场信息时,将考生信息存储入unit数组中,然后放入temp数组。在接收完一个考场信息之后,进行sorted排序操作。排序完之后为考场内每一位考生确定其考场排名。最后加入总的tests数组。

当所有信息都加入tests数组后,对tests数组进行排序,然后为每一位考生确定其总排名,最后输出。

这道题其实我十分钟就写好代码了,然后测试了一下就最后一个点运行超时。我试了一下大神的代码,发现最后一个测试点C++耗时40毫秒左右,按照往常的尿性,python应该是通过不了了。

但是,我花了三十分钟,给它优化了一下,诶,成了!

优化的步骤:

1>一开始我是使用类实例存储学员信息的。我为类添加了__slots__属性,最后一个测试点在最终输出前耗时下降了20MS

2>使用数组代替类实例存储学院信息。最后一个测试点在最终输出前耗时下降了20MS

3>重新排列数组顺序,去除掉可以忽视的类型转换,使用join连接数组组合字符串进行输出,终于成了

感言:

最后一步真的很重要,join连接数组进行输出真的很快。合理优化真的有些题目Python是可以通过的。

不过这可能也和我写代码的不良习惯相关?

最后吐槽一下,Python的输入真的慢的让人窒息。

Python实现

def main():
    n = int(input())
    tests = []
    count = 0
    for x in range(n):
        k = int(input())
        count += k
        temp = []
        for i in range(k):
            line = input().split(" ")
            unit = [line[0], -1, str(x+1), -1, int(line[1])]
            temp.append(unit)
        temp = sorted(temp, key = lambda i : (-i[4], i[0]))
        temp[0][3] = '1'
        for i in range(1, k):
            if temp[i][4] != temp[i-1][4]:
                temp[i][3] = str(i+1)
            else:
                temp[i][3] = temp[i-1][3]
        tests += temp
    tests = sorted(tests, key = lambda x:(-x[4], x[0]))
    tests[0][1] = '1'
    print(count)
    print(' '.join(tests[0][:-1]))
    for i in range(1, count):
        if tests[i][4] != tests[i-1][4]:
            tests[i][1] = str(i+1)
        else:
            tests[i][1] = tests[i-1][1]
            
        print(' '.join(tests[i][:-1]))
        
if __name__ == "__main__":
    main()

猜你喜欢

转载自blog.csdn.net/qq_35499060/article/details/82120577
今日推荐