#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 6);
int s, vis[maxn], kase;
double w[6], sumw[maxn], r;
struct Node
{
double l, r;
Node(double dl = 0, double dr = 0): l(dl), r(dr) {}
};
int bitcount(int x)
{
return (x == 0 ? 0 : bitcount(x / 2) + (x & 1));//输出x的二进制表示中的1的数量
}
std::vector<Node> node[maxn];
void dfs(int s)
{
if (vis[s]) return;
else vis[s] = 1;
if (bitcount(s) == 1)
{node[s].push_back(Node(0, 0)); return;}
for (int l = (s - 1)&s; l > 0; l = (l - 1)&s)
{
int r = s ^ l;
dfs(l); dfs(r);
for (int i = 0; i < node[l].size(); i++)
for (int j = 0; j < node[r].size(); j++)
{
double ll = min(-sumw[r] / (sumw[l] + sumw[r]) + node[l][i].l, sumw[l] / (sumw[l] + sumw[r]) + node[r][j].l);
double rr = max(sumw[l] / (sumw[l] + sumw[r]) + node[r][j].r, -sumw[r] / (sumw[l] + sumw[r]) + node[l][i].r);
node[s].push_back(Node(ll, rr));
}
}
}
void solve()
{
double ans = -1;
int si = (1 << s) - 1;
dfs(si);
for (int i = 0; i < node[si].size(); i++)
if (node[si][i].r - node[si][i].l < r && node[si][i].r - node[si][i].l > ans)
ans = node[si][i].r - node[si][i].l;
if (ans == -1) cout << -1 << endl;
else cout << setprecision(17) << ans << endl;
}
int main(int argc, char const *argv[])
{
cin >> kase;
while (kase--)
{
memset(vis, 0, sizeof(vis));
memset(node, 0, sizeof(node));
memset(sumw, 0, sizeof(sumw));
cin >> r >> s;
for (int i = 0; i < s; i++)
cin >> w[i];
for (int i = 0; i < (1 << s); i++)
for (int j = 0; j < s; j++)
if (i & (1 << j)) sumw[i] += w[j];
solve();
}
return 0;
}
uva 1354 位运算与搜索
最新推荐文章于 2020-08-13 14:29:41 发布