11th National Games python test question H: Q&A

Q & A


[Problem description]
There are n students looking for a teacher to answer questions at the same time. Every student estimated the time for answering questions in advance.
The teacher can arrange the order of answering questions, and students should enter the teacher’s office one by one to answer questions.
The process for a student to answer questions is as follows:
First enter the office, and the student numbered i needs si milliseconds. Then the students ask the teacher to answer the question, the student with the number i needs ai milliseconds. After the Q&A is completed, the students are very happy and will send a message in the course group. The time required can be ignored.
Finally, the classmates packed their things and left the office, which took ei milliseconds. Generally, it takes 10 seconds, 20 seconds or 30 seconds, that is, the value of ei is 10000, 20000 or 30000. After one student leaves the office, the next student can enter the office immediately.
Q&A starts from time 0. The teacher wants to arrange the order of answering questions reasonably so that the sum of the moments when students send messages in the course group is the smallest.
[Input format] The
first line of input contains an integer n, which represents the number of students.
The next n lines describe the time of each student. The i-th row contains three integers si, ai, ei, the meaning is
as described above.
[Output format]
Output an integer, indicating the minimum sum of the moments when students send messages in the course group.
[Sample input]
3
10000 10000 10000
20000 50000 20000
30000 20000 30000
[Sample output]
280000
[Sample description]
Answer questions in the order of 1, 3, 2, and the time of sending the message is 20000, 80000, and 180000 respectively.
[Evaluation use case scale and agreement]
For 30% of evaluation use cases, 1 ≤ n ≤ 20.
For 60% of the evaluation cases, 1 ≤ n ≤ 200.
For all evaluation use cases, 1 ≤ n ≤ 1000, 1 ≤ si ≤ 60000, 1 ≤ ai ≤ 1000000, ei ∈ {10000, 20000, 30000}, that is, ei must be one of 10000, 20000, or 30000.
Idea:
This question refers to the minimum time for the classmate from entering to the end. For example, the first classmate's time to enter is 10000 plus the message 10000, then this is the moment he finishes the message 20000. Xiaowei's classmate is the upper classmate The total time added to is the sum of the time he finished sending the message. The title refers to the minimum time. Then we only need to arrange the total time of each student from small to large, because it is the accumulation of time, not the time used Accumulation, so the less the total time used in the front, the smaller the sum of the time to send the message.
program:

n = int(input().strip())
s = [list(map(int,input().split())) for i in range(n)]
r = []
for i in range(n):
    r.append([sum(s[i]),s[i][2]])
r.sort()
m = 0
s1 = 0
for i in range(n):
    m += r[i][0]-r[i][1] + s1
    s1 += r[i][0]
print(m)

Reprinting is prohibited. Only for self-study. No responsibility for program errors.

Guess you like

Origin blog.csdn.net/weixin_46640345/article/details/112907744