Equilibrium Mobile(UVA 12166)

文章讲述了如何通过优化算法解决一个关于深度限制的二叉树问题,即在保证天平平衡的前提下,最少修改秤砣重量。作者发现通过将不同深度的节点值降至最低深度进行计算,简化了问题,只需要修改原节点值即可。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网址如下:

Equilibrium Mobile - UVA 12166 - Virtual Judge (vjudge.net)

(第三方网站)

很骚的一题,让我对二叉树有了新的认知

简单来说就是给一个深度不超过16的二叉树,代表一个天平。每根杆都悬挂在中间,每个秤砣的重量已知。至少修改多少个秤砣的重量才能让天平平衡?

虽然给的树都是正整数,但是修改的重量可以是浮点数

天平的图片可以看网址里面的

代码如下:

#include<cstdio>
#include<list>
#include<map>
#include<cctype>
#include<cmath>
using namespace std;
const double EPS = 1e-6;
struct Node{
    double v;
    int depth;
    Node(double v, int depth):v(v), depth(depth){}
};

int main(void)
{
    int kase; scanf("%d", &kase); getchar();
    while(kase--){
        map<double, int> s;
        list<Node> l;
        int d{}, maxd{}; char c;
        double ext{};
        //输入
        while((c = getchar()) != '\n'){
            if(isdigit(c)) ext = ext * 10.0 + c - '0';
            else{
                if(ext){l.push_back(Node(ext, d)); ext = 0.0;}
                if(c == '[') d++;
                else if(c == ']') d--;
                maxd = (maxd > d) ? maxd : d;
            }
        }
        if(ext) l.push_back(Node(ext, d));
        //处理
        int maxn = 0;
        for(auto it = l.begin(); it != l.end(); it++){
            double val = it->v / pow(2.0, maxd - it->depth);
            if(s.count(val)) s[val]++;
            else s[val] = 1;
            maxn = (maxn > s[val]) ? maxn : s[val];
        }
            
        //输出
        printf("%d\n", l.size() - maxn);
    }

    return 0;
}

我刚开始一直在想:这不是要尽可能地使相邻层的重量相差一倍嘛,但是这要怎么搞?

如果一个一个试着改变的话,我估计大概率会超时,而且很麻烦

最后是想出了这个办法:

使二叉树不同深度的地方全部降到最低深度,因为天平两边的杆相同,所以是每下降一层就除2

公式:

double val = it->v / pow(2.0, maxd - it->depth);
//it指向当前结点,val是下降后的值,v是原来的值,maxd是最大深度,depth是当前结点的深度

然后就是求一堆数中,至少要修改多少个数的值,才能使所有数相等

这很简单

但是为什么会转化成这样?

因为对于下降过后的结点,如果更改这个下降后的结点的值一次,就是更改原来的结点的值一次

而对于同一个结点下降后虽然会分成多个结点,但是这些下降后的结点的值都是相等的,而改变所有这些下降后的结点(归属同一个原来的结点)的值,只需要改变一次原来的结点的值就行了,所以干脆把这些结点变成一个结点看待

具体实现看我代码

### Deep Equilibrium Modeling in Machine Learning Deep equilibrium (DEQ) models represent a class of neural networks that focus on finding fixed points rather than stacking layers sequentially. These models are particularly useful when dealing with problems where depth plays an essential role but comes at significant computational costs. In DEQs, instead of explicitly constructing deep architectures layer-by-layer, the network computes solutions as implicit functions defined through fixed-point iterations. The core idea behind DEQ lies in solving equations such as \( f(x; \theta) = x \), where \( x \) represents the output and \( \theta \) denotes parameters learned during training. This approach allows for efficient memory usage since no intermediate activations need storage across multiple layers[^1]. Training involves optimizing objectives based on these fixed points while ensuring stability conditions hold true throughout computation processes. In terms of implementation within machine learning frameworks like PyTorch or TensorFlow: ```python import torch from scipy.optimize import root_scalar def deq_forward(z_init, func, theta): """Forward pass function.""" def eqn(z): return z - func(z, theta) sol = root_scalar(eqn, bracket=[-1e3, 1e3], method='brentq') if not sol.converged: raise RuntimeError('Fixed point iteration did not converge.') return sol.root class DEQLayer(torch.nn.Module): def __init__(self, func): super().__init__() self.func = func def forward(self, inputs): outputs = deq_forward(inputs, self.func, self.theta) return outputs ``` This code snippet demonstrates how one might implement a basic version of a DEQ layer leveraging Python's `scipy` library alongside PyTorch tensors. Note this example simplifies actual implementations found in research papers which often include additional considerations regarding numerical precision and convergence guarantees. Regarding reinforcement learning contexts specifically mentioned earlier via references provided about dueling networks & value estimation techniques [^4][^3]: One could extend traditional Q-learning algorithms incorporating ideas from DEQ theory into policy evaluation steps aiming towards more stable representations over time without needing explicit temporal difference updates per se given proper formulations aligning well under certain assumptions concerning environment dynamics along Markov Decision Processes definitions typically utilized therein scenarios involving sequential decision makings tasks among others aspects potentially explored further depending upon specific application requirements accordingly then too!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值