Equilibrium Mobile

Problem Description


A mobile is a type of kinetic sculpture constructed to take advantage of the principle of equilibrium. It consists of a number of rods, from which weighted objects or further rods hang. The objects hanging from the rods balance each other, so that the rods remain more or less horizontal. Each rod hangs from only one string, which gives it freedom to rotate about the string.

We consider mobiles where each rod is attached to its string exactly in the middle, as in the figure underneath. You are given such a configuration, but the weights on the ends are chosen incorrectly, so that the mobile is not in equilibrium. Since that‘s not aesthetically pleasing, you decide to change some of the weights. 

What is the minimum number of weights that you must change in order to bring the mobile to equilibrium? You may substitute any weight by any (possibly non-integer) weight. For the mobile shown in the figure, equilibrium can be reached by changing the middle weight from 7 to 3, so only 1 weight needs to changed.

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

* One line with the structure of the mobile, which is a recursively defined expression of the form:

<expr> ::= <weight> | "[" <expr> "," <expr> "]"

with <weight> a positive integer smaller than 109 indicating a weight and [<expr>,<expr>] indicating a rod with the two expressions at the ends of the rod. The total number of rods in the chain from a weight to the top of the mobile will be at most 16.

Output

Per testcase:

* One line with the minimum number of weights that have to be changed.

Sample Input

3
[[3,7],6]
40
[[2,3],[4,5]]

Sample Output

1
0
3

这道题重点在于寻找基准数,例如

,以三为基准数,只需将7改成3。

基准数,即多少个可以不变的数,首先应该进行枚举,把每一个点都看作基准数,具体怎么改并不需要理会。

那么怎样才能知道最少应该该多少个呢?

把每一个点都看做基准数,可知最多要改多少个。

然后~

有一些不同的基准数,最后根据它们而改造成的平衡二叉树的总重量是相同的,所以当以这些基准数的其中一个作为基准来调整树时,其他的基准数是不需要变化的,也就是说这些造成

树的总重量相同的数都不需要改,那么不需要改的越多,需要改的就越少。

那么:

公式:由一颗平衡二叉树的某个节点求它的总重量:

设这个节点为w,深度为d,总重量为ww

ww=w*pow(2,d),即ww=w<<d

这道题完全是借鉴别人博客才明白的:http://blog.csdn.net/crazysillynerd/article/details/43876123

基本一样~~唉,没办法,自己太low了,不过好歹也敲了一遍,加深了理解。

#include"iostream"
#include"cstdio"
#include"cstring"
#include"map"
using namespace std;

map<long long,int>book;
int sum;

string a;

void biaoji(int depth,int b,int e)
{
    if(a[b]==‘[‘)
    {
        int p=0;
        for(int i=b+1;i<e;i++)
        {
        if(a[i]==‘[‘) p++;
        if(a[i]==‘]‘) p--;
        if(p==0&&a[i]==‘,‘)
        {
            biaoji(depth+1,b+1,i-1);
            biaoji(depth+1,i+1,e-1);
        }
        }
    }

    else
    {
        long long w=0;
        for(int j=b;j<=e;j++) w=w*10+a[j]-‘0‘;
        sum++;
  //     cout<<w<<endl;
        book[w<<depth]++;
    }

}

int main()
{
    int T;
    cin>>T;
  //  getchar();
    while(T--)
    {
        cin>>a;
        book.clear();
        sum=0;
      //  cout<<ch<<endl;
        biaoji(0,0,a.size()-1);
        int ans=0;
        map<long long,int>::iterator it;
        for(it=book.begin();it!=book.end();it++) ans=max(ans,it->second) ;
    //   cout<<sum<<endl;
       cout<<sum-ans<<endl;

    }
    return 0;
}
时间: 2024-07-30 18:20:09

Equilibrium Mobile的相关文章

UVA - 12166 Equilibrium Mobile (修改天平)(dfs字符串表示的二叉树)

题意:问使天平平衡需要改动的最少的叶子结点重量的个数. 分析:天平达到平衡总会有个重量,这个重量可以由某个叶子结点的重量和深度直接决定. 如下例子: 假设根结点深度为0,结点6深度为1,若以该结点为基准(该结点值不变),天平要平衡,总重量是12(6 << 1),而若以结点3为基准,天平要平衡,总重量也是12(3 << 2). 由此可得,只需要算出以每个结点为基准的总重量,若该重量下对应的叶子结点最多,则使天平在此重量下平衡改变的叶子结点数最少. #pragma comment(li

UVA 12166 Equilibrium Mobile

直接贪心嘛.先想想最后平衡的时候,如果知道了总重量,那么每一个结点的重量其实也就确定了. 每个结点在左在右其实都不影响,只和层数有关.现在反过来,如果不修改某个结点,那么就可以计算出总质量,取总质量出现次数最多的保持不变. /********************************************************* * --------------Tyrannosaurus--------- * * author AbyssalFish * ***************

UVA-12166 Equilibrium Mobile(二叉树)

题目大意:改变二叉树上的权值,使树平衡,问最少该几个值. 题目分析:不会做,查的题解.有条奇妙的性质:如果将第d层权值为w的节点为基准做改动,则整棵树的总重量为w<<d,即w*2^d.仔细一想,确实是这样的. 代码如下: # include<iostream> # include<cstdio> # include<map> # include<cstring> # include<algorithm> using namespace

UVa12166 Equilibrium Mobile (二叉树结论)

链接:http://acm.hust.edu.cn/vjudge/problem/24840分析:以二叉树的某个结点的重量为基准(基准的意思是它的重量不改变),要使整颗二叉树重量达到平衡,整棵树的总重量为w<<depth(w为基准重量,depth为结点的深度从0开始算).那么统计出叶子结点的个数,并且算出以每个叶子结点的重量为基准时整棵树的总重量,把每种总重量出现的次数用map存起来,然后反过来想,要使整颗树在某个总重量下达到平衡,根据前面的(w<<depth)的推导,结点的深度和

12166 - Equilibrium Mobile(DFS)

枚举每个点,判断其余深度是否存在点的权值为(1 << (deep1 - deep2)) * x deep1,deep2为2点的深度 写的比较麻烦 #include<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int maxn = 1000005; char str[ma

Not so Mobile

Not so Mobile Before being an ubiquous communications gadget, a mobile was just a structure made of strings and wires suspending colourfull things. This kind of mobile is usually found hanging over cradles of small babies. The figure illustrates a si

uva 839 not so mobile——yhx

Not so Mobile  Before being an ubiquous communications gadget, a mobile was just a structure made of strings and wires suspending colourfull things. This kind of mobile is usually found hanging over cradles of small babies. (picture copy failed,cou h

UVa 839 Not so Mobile(树的递归输入)

题意  判断一个树状天平是否平衡   每个测试样例每行4个数  wl,dl,wr,dr  当wl*dl=wr*dr时  视为这个天平平衡  当wl或wr等于0是  下一行将是一个子天平  如果子天平平衡  wl为子天平的wl+wr  否则整个天平不平衡 容易看出  输入是递归的  所以我们可以直接递归  边输入边判断 #include<cstdio> using namespace std; bool solve(int &w) { int wl, dl, wr, dr; bool m

G - Not so Mobile

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Before being an ubiquous communications gadget, a mobile was just a structure made of strings and wires suspending colourfull things. This kind of mobile is usually fo