1015 theory of virtue and talent (25 points) python code timeout, ideas can be used for reference, very easy to understand

Posted by nobodyk on Sat, 25 Sep 2021 11:11:11 +0200

Sima Guang, a historian of the Song Dynasty, wrote a famous "theory of virtue and talent" in Zizhi Tongjian: "therefore, the perfection of talent and morality is called a saint, the death of talent and morality is called a fool, the victory of virtue is called a gentleman, and the victory of virtue is called a villain. If a gentleman takes the skill of man, he can't be a saint. If a gentleman takes it, he won't be a fool rather than a villain."

The scores of virtue and talent of a group of candidates are given. Please give the admission ranking according to Sima Guang's theory.

Input format:

Input the first line and give 3 positive integers, respectively: N (≤ 105), that is, the total number of candidates; L (≥ 60) is the lowest score line for admission, that is, both moral score and talent score are not lower than   L   Candidates are eligible to be considered for admission; H (< 100) is the priority admission line - those whose moral score and talent score are not lower than this line are defined as "full of talent and virtue", and such candidates are ranked from high to low according to the total score of morality and talent; The candidates who can't get the talent score but get the moral score line belong to "virtue wins talent", which is also sorted according to the total score, but they are ranked behind the first category of candidates; Both moral and talent scores are lower than   H. However, candidates whose moral score is not lower than that of talent belong to "both talent and morality" but still have "virtue wins talent", which are sorted according to the total score, but ranked behind the second category of candidates; Others reach the lowest line   L   Of candidates are also ranked according to the total score, but behind the third category of candidates.

subsequently   N   Line, each line gives the information of one candidate, including: admission card number, German score, in which the admission card number is an 8-digit integer, and German score is an integer in the interval [0, 100]. Numbers are separated by spaces.

Output format:

The first line of output first gives the number of candidates who have reached the lowest score line   M. Then   M   Each line outputs the information of one candidate according to the input format, and the candidates are sorted from high to low according to the rules described in the input. When there are many candidates with the same total score, they are arranged in descending order according to their moral score; If the scores are also in parallel, they will be output in ascending order of the admission number.

Input example:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

No blank lines at the end

Output example:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

No blank lines at the end

python code: (there is a timeout during operation, which has not been solved yet, but the idea is very clear and easy to understand)

n,l,h=map(int,input().split())#n is the total number of candidates; l is the lowest admission score line and H is the priority admission line

a=[[0 for i in range(4)] for i in range(n)]#Two dimensional empty array n (14) x4

for i in range(n):#input data
    a[i][0],a[i][1],a[i][2]=map(int,input().split())
# print(a)


m= 0  # Number of candidates reaching the lowest score line
for i in range(n):#Update the a array, and the first m elements in a are the output samples to be sorted
     if a[i][1]>=l and a[i][2]>=l:
         m+=1
         a[m-1][0], a[m-1][1], a[m-1][2]=a[i][0],a[i][1],a[i][2]#Update a array
         a[m - 1][3] = a[i][1] + a[i][2]#Write down the sum of virtue and talent

# print(a) #The first m elements in a are the output samples to be sorted

list1=[]#Store a class of examinees, whose virtues and talents are greater than 80
list2=[]#Only candidates who store virtue win, and only those whose virtue is greater than 80 are less than 80
list3=[]#Candidates who reach the bottom line have more virtue than talent
list4=[]#Candidates who reach the bottom line are less virtuous than talented

def sortlist(list):#Sort:#First arrange the total score, and then look at the German score
    for j in range(len(list)):
        for i in range(j, len(list)):
            if list[j][3] < list[i][3]:
                list[j], list[i] = list[i], list[j]
            elif list[j][3] == list[i][3]:
                if list[j][1] <= list[i][1]:
                    list[j], list[i] = list[i], list[j]

for i in range(m):#Start classification
    if a[i][1] >= h and a[i][2] >= h:
        list1.append(a[i])
    elif a[i][1] >= h and a[i][2] < h:
        list2.append(a[i])
    elif a[i][1] < h :
        if a[i][1] > a[i][2]:
            list3.append(a[i])
        else:
            list4.append(a[i])

sortlist(list1)
# print(list1)#13,12,3,11,4
sortlist(list2)
# print(list2)#7,6,5,2
sortlist(list3)
# print(list3)#14
sortlist(list4)
# print(list4)#8,1

def out_put(list):#output
    for i in range(len(list)):
        print("%d %d %d" % (list[i][0], list[i][1], list[i][2]))

print(m)
out_put(list1)
out_put(list2)
out_put(list3)
out_put(list4)

c + + code will be supplemented later~~~

Topics: Python