Uva 1354 Mobile Computing

题目链接

题意:

  在一个宽为r 的房间里, 有s个砝码, 每个天平的一端要么挂砝码, 要么挂另一个天平, 并且每个天平要保持平衡。

  求使得所有砝码都放在天平上, 且总宽度不超过房间宽度的最大值。

思路:

  每个节点只能有两个子节点, 这是一棵二叉树的形式。

  通过枚举二叉树的形态, 再枚举每一个叶子节点所放砝码, 最后再计算每个方案的宽度并计算答案。

  每增加一个天平, 那么可以放砝码数 + 1。

note:

  坑在0的输出了, 用primtf("%.9lf\n", 0)输出来的是0  用0.0来输出才是0.000000 惨wa三发。

代码:

  

  1 #include <cmath>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <ctime>
  6 #include <set>
  7 #include <map>
  8 #include <list>
  9 #include <stack>
 10 #include <queue>
 11 #include <string>
 12 #include <vector>
 13 #include <fstream>
 14 #include <iterator>
 15 #include <iostream>
 16 #include <algorithm>
 17 using namespace std;
 18 #define LL long long
 19 #define INF 0x3f3f3f3f
 20 #define MOD 1000000007
 21 #define eps 1e-5
 22 #define MAXN 110
 23 #define MAXM 100
 24 #define dd {cout<<"debug"<<endl;}
 25 #define pa {system("pause");}
 26 #define p(x) {cout<<x<<endl;}
 27 #define pd(x) {printf("%.7lf\n", x);}
 28 #define k(x) {printf("Case %d: ", ++x);}
 29 #define s(x) {scanf("%d", &x);}
 30 #define sd(x) {scanf("%lf", &x);}
 31 #define mes(x, d) {memset(x, d, sizeof(x));}
 32 #define do(i, x) for(i = 0; i < x; i ++)
 33 #define dod(i, x, l) for(i = x; i >= l; i --)
 34 #define doe(i, x) for(i = 1; i <= x; i ++)
 35 int n;
 36 double r, ans;
 37 double w[MAXN], v[MAXN];
 38 double ll[MAXN], rr[MAXN];
 39 bool vis[MAXN];
 40 int order[MAXN];
 41 void read()
 42 {
 43     scanf("%lf", &r);
 44     scanf("%d", &n);
 45     for (int i = 1; i <= n; i ++)
 46         scanf("%lf", &w[i]);
 47 }
 48 void get_ans(int u)
 49 {
 50     memset(ll, 0, sizeof(ll));
 51     memset(rr, 0, sizeof(rr));
 52     memset(v, 0, sizeof(v));
 53
 54     for (int i = u; i > 0; i --)
 55     {
 56         if (order[i] == -1)
 57         {
 58             int x = i * 2;
 59             int y = i * 2 + 1;
 60             v[i] = v[x] + v[y];
 61             double li = v[y] / v[i];
 62             double ri = v[x] / v[i];
 63
 64             ll[i] = min(-li + ll[x], ri + ll[y]);
 65             rr[i] = max(-li + rr[x], ri + rr[y]);
 66         }
 67         else if (order[i])
 68         {
 69             v[i] = w[order[i]];
 70         }
 71     }
 72
 73     double temp = rr[1] - ll[1];
 74     //printf("%.9lf\n", temp);
 75     if (temp - r < eps && temp > ans)
 76         ans = temp;
 77 }
 78
 79 void dfs(int u, int num, int count)
 80 {
 81     //printf("%d %d %d\n", u, num, count);
 82     if (count == 0)
 83     {
 84         get_ans(u - 1);
 85         return ;
 86     }
 87     else if (order[u / 2] != -1)
 88     {
 89         dfs(u + 1, num, count);
 90     }
 91     else
 92     {
 93         if (count > num)
 94         {
 95             order[u] = -1;
 96             dfs(u + 1, num + 1, count);
 97             order[u] = 0;
 98         }
 99
100         if (num == 1 && count > 1)
101             return ;
102         for (int i = 1; i <= n; i ++)
103             if (!vis[i])
104             {
105                 vis[i] = true;
106                 order[u] = i;
107                 dfs(u + 1, num - 1, count - 1);
108                 order[u] = 0;
109                 vis[i] = false;
110             }
111     }
112 }
113 void solve()
114 {
115     memset(vis, false, sizeof(vis));
116     memset(order, 0, sizeof(order));
117     ans = -1;
118     if (n == 1) printf("%.10lf\n", 0.0);
119     else
120     {
121         order[1] = -1;
122         dfs(2, 2, n);
123         printf(ans == -1? "-1\n" : "%.10lf\n", ans);
124     }
125 }
126
127 int main()
128 {
129     int T;
130     scanf("%d", &T);
131     while (T --)
132     {
133         read();
134         solve();
135     }
136     return 0;
137 }

时间: 2024-10-21 13:52:53

Uva 1354 Mobile Computing的相关文章

UVa 1354 Mobile Computing[暴力枚举]

**1354 Mobile Computing** There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful stones on the planet, and the Yaen people love to collect them. They bring the stones back home and make nice mobile arts of th

UVa 1354 Mobile Computing | GOJ 1320 不加修饰的天平问题

传送门1(UVa): https://uva.onlinejudge.org/external/13/1354.pdf 传送门2(GOJ): http://acm.gdufe.edu.cn/Problem/read/id/1320 题意: 长度限制 r (1 < r < 10), 给 n (1 <= n <= 6) 个砝码,组成平衡(考虑重量和力臂)的天平,求天平最长能多长. 2015个人选拔赛#6 1004 比赛的时候完全不知道怎么做,比赛完两天重新看一遍有点思路就是敲不出来(弱

uva 1354 Mobile Computing ——yhx

1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 struct node 5 { 6 int fat,lson,rson; 7 double wei; 8 }tree[500]; 9 double w[10],lim,ans; 10 int n; 11 double max(double x,double y) 12 { 13 return x>y?x:y; 14 } 15 void calc_s

UVA 1354 Mobile Computing(天平难题,枚举子集,递归,好题*)

1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 /** 6 思路:在每一个根节点枚举左右子树 7 8 学习: 9 (1)枚举子集的方法 例如 枚举 s = 100101 的子集 10 for(int l = (s-1)&s , l > 0 ; l = (l-1) & s){ 11 int r = s ^ l; 12 (2)思考:如何表示 左边距离 右边距离 . 该点的

1354 Mobile Computing(暴力、二进制枚举、简直无情)

翘了3节课来A这道题,最后还超时了,也是蛮拼的.. 没做出来主要一个方面就是不会一个二进制数子集的枚举 这里上一下代码: for(int S0 = S; S0; S0 = (S0 - 1) & S){ } 这里S0就是S的子集了~! 题目的思路就是枚举所有情况,注意记忆化[话说这题学到了不少] #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using n

UVa 1354 天平难题

首先呈现刘汝佳的高级代码 // UVa1354 Mobile Computing // Rujia Liu #include<cstdio> #include<cstring> #include<vector> using namespace std; struct Tree { double L, R; // distance from the root to the leftmost/rightmost point Tree():L(0),R(0) {} }; co

MITG2102 Mobile Computing (Section 51)

College of International Education (CIE)Associate Degree ProgrammeProject on Mobile Applications Development Course : MITG2102 Mobile Computing (Section 51)Year/Term : 2018-19, Semester 2Deadline : 2 May, 2019 (Thu) 17:30 onto MoodleCredit : 50% of t

CSE 535: Mobile Computing

CSE 535: Mobile ComputingAccelerometer Data GraphAssignment 2Purpose:Assignment 2 will be an extension of Assignment 1. In this assignment, you will be developing adatabase for a patient. There are 3 parts to complete for Assignment 2. The components

UVa 1354 枚举子集 Mobile Computing

只要枚举左右两个子天平砝码的集合,我们就能算出左右两个悬挂点到根悬挂点的距离. 但是题中要求找尽量宽的天平但是不能超过房间的宽度,想不到要怎样记录结果. 参考别人代码,用了一个结构体的vector,保存每个集合合法方案的左右两端最长的距离. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <map> 6 #