UVa 12166 - Equilibrium Mobile <二叉树+DFS>

本文介绍了一种解决平衡移动雕塑(Mobiles)问题的算法,旨在通过最少次数的重量更改使雕塑达到平衡状态。该算法适用于由多个杆组成的雕塑结构,并能够处理递归定义的配置。文中提供了一个具体的实现示例,包括输入输出格式及样例。

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


#include <bits/stdc++.h>
using namespace std;

int cur = 0;
char str[1024000];
map<long long, int> cnt;

void DFS(int dep)
{
    if(isdigit(str[cur])){
        long long a = 0;
        while(isdigit(str[cur]))
            a = a * 10 + str[cur++] - '0';
        cnt[a<<dep]++;
    }
    else{
        cur++; DFS(dep + 1);
        cur++; DFS(dep + 1);
        cur++;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    int T; cin >> T; cin.get();
    while(cur = 0, cnt.clear(), T--) {
        cin >> str; DFS(0);
        int mx = INT_MIN, sum = 0;
        for(map<long long, int>::iterator it = cnt.begin(); it != cnt.end(); it++)
            sum += it->second, mx = max(mx, it->second);
        cout << sum - mx << endl;
    }
    return 0;
}



这是一个关于非线性系统稳定性的结论。假设我们有一个非线性系统的状态方程为 x' = f(x),其中 x 是系统的状态向量,f 是可微的非线性函数。如果我们将系统的状态平衡点设置为 x=0,那么系统的稳定性取决于其局部行为。 现在假设我们有一个满足条件 If V(x)的积分 < -BV(x),其中 B>0,V(x)是一个正定的、二次型的函数,表示系统的能量函数。这个条件意味着系统的能量函数在系统的状态空间中是严格递减的,且递减速度至少是 BV(x)。因此,系统的能量函数在平衡点 x=0 处是指数级别的稳定。 为了更好地理解这个结论,我们可以考虑一个简单的例子。假设我们有一个单摆系统,其状态向量为 x=[&theta;,&theta;'],其中 &theta; 是摆的角度,&theta;' 是角速度。我们可以定义系统的能量函数为 V(x) = 1/2 &theta;'^2 + g cos(&theta;),其中 g 是重力加速度。这个能量函数是一个正定的、二次型的函数,表示系统的总能量。如果我们计算 If V(x)的积分 < -BV(x),其中 B>0,我们可以得到条件为 cos(&theta;) > 0,即摆的初始角度必须小于 π/2。这个条件意味着系统的能量函数在摆的最低点处是最小的,也就是说,系统的能量函数在平衡点处是稳定的。因此,当我们扰动摆的初始角度时,系统会回到平衡点处,且回到平衡点的速度是指数级别的。 综上所述,如果满足 If V(x)的积分 < -BV(x) 这个条件,那么系统的平衡点 x=0 是指数级别稳定的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值