• Equilibrium Mobile(UVA-12166)


    Equilibrium Mobile(UVA-12166)

    Problem

    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 gure underneath. You are given such a conguration, 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 gure, equilibrium can be reached by changing the middle weight from 7 to 3, so only 1 weight needs to changed.

    Input:

    On the rst 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 dened 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
    
    Solution

    思维题,对于这颗树,每个节点的左右子树应该时平衡的,同时这个节点的左右子树的重量应该相等,在纸上画画很容易看出,我们最后会将这颗树变化一颗可以看作是具有相同权值的叶子节点的完全二叉树,返回到原来的树上,我们就可以根据叶子节点的深度和叶子节点的值确定这颗二叉树的权重,每个叶子节点均是如此,对于二叉树权值最后相同的叶子节点来说,他们是想通过的,也就是在改变过程中不需要改变的,所以我们统计二叉树权值的众数和所有叶子节点的个数,就可以计算出最少需要改变多少即:叶子总数-二叉树权值众数。

    Code
    /**********************************************************
    * @Author: 			   Kirito
    * @Last Modified by:   Kirito
    * @Remark:
    **********************************************************/
    #include <bits/stdc++.h>
    #define lowbit(x) (x&(-x))
    #define CSE(x,y) memset(x,y,sizeof(x))
    #define INF 0x3f3f3f3f
    #define Abs(x) (x>=0?x:(-x))
    #define FAST ios::sync_with_stdio(false);cin.tie(0);
    using namespace std;
    
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pii;
    typedef pair<ll , ll> pll;
    
    const int MAXN=1111111;
    
    void build(string str,ll &num,unordered_map<ll,ll> &cnt)
    {
        int len=str.length();ll depth=0;
        int ad=0;
        while(ad<len){
            if(str[ad]=='['){
                depth++;
                ad++;
            }
            else if(str[ad]==']'){
                depth--;
                ad++;
            }
            else if(str[ad]==','){
                ad++;
            }
            else{
                num++;
                ll ans=0;
                while(ad<len&&str[ad]>='0'&&str[ad]<='9'){
                    ans=ans*10+ull(str[ad++]-'0');
                }
                cnt[ans*(1<<depth)]++;
            }
        }
        return;
    }
    
    int main()
    {
    	#ifndef ONLINE_JUDGE
    	freopen("in.in","r",stdin);
    	#endif
    	FAST;
        int n;
        cin>>n;
        while(n--){
            string str;
            unordered_map<ll,ll> cnt;
            ll num=0;
            ll mx=0;
            cin>>str;
            build(str,num,cnt);
            for(unordered_map<ll,ll>::iterator it=cnt.begin();it!=cnt.end();it++){
                mx=max(it->second,mx);
            }
            cout<<num-mx<<endl;
        }
    	return 0;
    }
    
  • 相关阅读:
    android: 建立和断开a2dp link 相关方法总结
    Java: volatile和synchronized的区别 (转)
    shell 将变量当命令执行的几种方式
    tcp 自连接(主机的端口和自己建立连接)
    sed使用
    海尔风冷冰箱冷冻室最底层结冰 问题解决
    k8s 传参数的两种方式一种是 环境变量 拼接 另一种说是yaml传参到配置文件的表示怀疑要验证????????????????????????????
    k8s 集群中的etcd故障解决
    dockerfile
    使用VIM/VI给文件加密和解密
  • 原文地址:https://www.cnblogs.com/LeafLove/p/13606804.html
Copyright © 2020-2023  润新知