天平难题

来源:互联网 发布:linux telnet 类似 编辑:程序博客网 时间:2024/04/28 05:12
#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int maxn = 7;struct Tree{double l, r;Tree() : l(0), r(0) {}};bool vis[1<<maxn];double r, w[maxn], sum[1<<maxn];vector<Tree> tree[1<<maxn];void dfs(int root){if(vis[root]) return;vis[root] = true;bool have_kid = false;for(int left = (root-1)&root; left; left = (left-1)&root){have_kid = true;int right = root^left;double d1 = sum[right]/sum[root], d2 = sum[left]/sum[root];dfs(left); dfs(right);for(int i = 0; i < tree[left].size(); i++){for(int j = 0; j < tree[right].size(); j++){Tree t;t.l = max(d1+tree[left].l, tree[right].l-d2);t.r = max(d2+tree[right].r, tree[left].r-d1);if(t.l+t.r < r) tree[root].push_back(t);}}}if(!have_kid) tree[root].push_back(Tree());}int main(){int n;scanf("%lf%d", &r, &n);for(int i = 0; i < n; i++) scanf("%lf", &w[i]);for(int i = 0; i < (1<<n); i++){tree[i].clear();sum[i] = 0;for(int j = 0; j < n; j++)if((1<<j) & i) sum[i] += w[j];}int root = (1<<n)-1;memset(vis, 0, sizeof(vis));dfs(root);double ans = -1;for(int i = 0; i < tree[root].size(); i++)ans = max(ans, tree[root][i].l+tree[root][i].r);printf("%.10lf\n", ans);return 0;}

0 0