E21728: 排队做实验(Easy)
implementation, http://cs101.openjudge.cn/practice/21728/
某学院的学生都需要在某月的1号到2号期间完成课程实验,他们每个人的实验需要持续不同的时长。而实验室管理员要安排这些学生,按照一定顺序,在1号到2号期间全部完成实验。假设该学院的实验室同时只能容纳一名学生实验,并且每位学生都很积极,都想被排在第一位早点做完实验。
假设该学院有n个学生,实验室管理员收到了n个学生s1,s2,..,sn每位需要占用实验室的时长T1,T2,...,Tn,并且准备按照学生发邮件预约实验室的先后时间,安排实验顺序,现在他想要知道所有学生的平均等待时间,以此衡量先到先得的安排是否合理,请问你能帮他求出这个平均等待时间吗?
注意:学生编号从1到n
输入
输入为3行 第一行为n,为学生人数(n<=1000) 第二行为n个整数,分别表示第一位学生到第n位学生的实验时长T1,T2,...,Tn,每个数据间有一个空格 第三行为n个整数,表示按先到先得排出的学生实验顺序s1,s2,...,sn,每个数据间有一个空格,si代表第i个学生
输出
输出为1行 第一行为这种先到先得排序方案下,所有学生的平均等待时间(精确到小数点后两位)
样例输入
Sample Input1:
10
81 365 72 99 22 7 444 203 1024 203
6 5 3 1 4 8 10 2 7 9
Sample Output1:
431.90样例输出
Sample Input2:
18
490 329 970 969 435 981 839 177 616 857 583 551 443 565 393 237 804 398
8 16 2 15 18 5 13 1 12 14 11 9 17 7 10 4 3 6
Sample Output2:
3750.89提示
等待时间的定义是从时刻0开始的。第i个学生要等待前面i-1个学生都做完实验.
来源
cs101 2020 Final Exam
python
def main():
n = int(input())
# Read experiment durations. There are n numbers.
durations = list(map(int, input().split()))
# Read the order of students. There are n numbers.
order = list(map(int, input().split()))
total_waiting_time = 0
current_time = 0
# Process students in the given order.
for student in order:
total_waiting_time += current_time
# Convert student id (1-indexed) to index (0-indexed)
current_time += durations[student - 1]
average_waiting_time = total_waiting_time / n
# Output the average waiting time rounded to two decimals.
print(f"{average_waiting_time:.2f}")
if __name__ == "__main__":
main()python
n = int(input())
t = [int(i) for i in input().split()] #time
r = [int(i) for i in input().split()] #rank
dp = [0]*n
dp[0] = 0
for i in range(1, n):
dp[i] = dp[i-1] + t[r[i-1] - 1]
print('{:.2f}'.format(sum(dp)/n))