列表

详情


NC220429. BinarySeating

描述

By accident, two rooms (room 0 and room 1) got booked for the theoretical exam of the B++ Applied Programming Course and both were communicated to the students. Now students might go to either of the rooms, and as a student assistant your job is to supervise room 1. Since you assisted all these students during the course, you know how much time each student will need to fifinish the exam. Already before the exam you are eager to go home, but you can only leave when all of the students in your examination room have fifinished. You assume that every student chooses one of the exam rooms with equal probability, independent of the other students. After how much time do you expect to be able to leave?

输入描述

The input consists of:
    •A line with an integer n (1 ≤ n ≤ 40), the number of students.
    •A line with n integers t1, . . . , tn (1 ≤ ti ≤ 1000): tiis the time it takes for the ith student to fifinish the exam and leave.


输出描述

Output the expected time before you can leave. Your answer should have an absolute or relative error of at most 10−6.

示例1

输入:

2
2 3

输出:

2

示例2

输入:

5
1 4 5 2 3

输出:

4.03125

示例3

输入:

5
2 1 1 1 1

输出:

1.46875

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 3ms, 内存消耗: 476K, 提交时间: 2021-04-11 17:12:10

#include <bits/stdc++.h>
using namespace std;
int n,a[100];
double ans,t=0.5;
int main(){
	cin>>n;
	for(int i=0;i<n;i++) cin>>a[i];
	sort(a,a+n);
	for(int i=n-1;i>=0;i--){
		ans+=t*a[i];
		t/=2;
	}
	printf("%.6lf\n",ans);
}

pypy3(pypy3.6.1) 解法, 执行用时: 45ms, 内存消耗: 22736K, 提交时间: 2021-04-14 09:06:50

n = int(input())
a = map(int, input().split())

i, ans = 0, 0
for x in sorted(a):
    ans = ans + x * (1 << i) / (1 << n)
    i += 1
print(ans)

Python3(3.9) 解法, 执行用时: 41ms, 内存消耗: 6872K, 提交时间: 2021-04-11 14:16:21

n = int(input())
t = list(map(int,input().split()))

t.sort()

fm = 2**n

s = 0
for i in range(n):
    s+=2**i*t[i]
    
jg = s/fm

print(jg)

上一题