NC213957. RikkawithBook
描述
输入描述
The first line contains a single integer , the number of books.
The second line contains n integers , representing the length of each book.
The third line contains n integers , representing the weight of each book.
输出描述
Output a single line with a single real number, representing the maximum possible strangeness value.
Your answer should have an absolute error or relative error of less than .
示例1
输入:
4 2 2 2 2 1 1 1 1
输出:
2.08333333333
说明:
For the first sample, one optimal plan is shown in the following figure.示例2
输入:
3 1 2 3 3 2 1
输出:
2.95833333333
pypy3 解法, 执行用时: 787ms, 内存消耗: 46208K, 提交时间: 2022-11-18 21:23:34
n = int(input()) l = list(map(float, input().split())) w = list(map(float, input().split())) sum_w = [0] * (1 << n) for s in range(1, 1 << n): for i in range(n): if s >> i & 1: sum_w[s] = sum_w[s ^ (1 << i)] + w[i] break f = [0] * (1 << n) for s in range(1, 1 << n): for i in range(n): if s >> i & 1: t = s ^ (1 << i) f[s] = max(f[s], f[t] + w[i] / sum_w[s] * l[i] / 2) f[s] = max(f[s], l[i] * (1 - w[i] / sum_w[s] / 2)) print(f[-1])
C++(clang++11) 解法, 执行用时: 252ms, 内存消耗: 8576K, 提交时间: 2020-11-22 11:05:04
#include<bits/stdc++.h> using namespace std; typedef double db; db dp[1<<20],v[20],l[20]; int n; int main(){ cin >> n; for (int i=0;i<n;i++) cin >> l[i]; for (int i=0;i<n;i++) cin >> v[i]; for (int i=1;i<(1<<n);i++){ db w=0; for (int j=0;j<n;j++) if (i&(1<<j)) w+=v[j]; for (int j=0;j<n;j++) if (i&(1<<j)){ dp[i]=max(dp[i],dp[i^(1<<j)]+v[j]/w*l[j]/2); dp[i]=max(dp[i],l[j]-v[j]/w*l[j]/2); } } printf("%.10f\n",dp[(1<<n)-1]); return 0; }