【习题 6-6 UVA - 12166 】Equilibrium Mobile

本文介绍了一种使用递归深度优先搜索(DFS)解决特定类型问题的方法,并结合map数据结构进行统计,以求得最优解。通过遍历树形结构,确定每个节点及其父节点的权重关系,实现高效计算。

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

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


枚举一个秤砣的重量不变。
某一个秤砣的重量不变之后。
所有秤砣的重量就固定了。
因为它的兄弟节点的重量要和它一样。
则父亲节点的重量就是这个节点的两倍了。
以此类推可以得到所有节点的重量的值。
第i层应该的重量都是相同的。
用一个map[height][x]统计某层里面有多少个数字x即可。
这样只要循环层数次就好。
复杂度够过了。

【代码】

/*
    1.Shoud it use long long ?
    2.Have you ever test several sample(at least therr) yourself?
    3.Can you promise that the solution is right? At least,the main ideal
    4.use the puts("") or putchar() or printf and such things?
    5.init the used array or any value?
    6.use error MAX_VALUE?
    7.use scanf instead of cin/cout?
*/
#include <bits/stdc++.h>
using namespace std;

string s;
int n,total;
map <long long,int> mmap[20];
map <int,vector <int> > mmap2;

void dfs(int l,int r,int dep){
    if (s[l]=='['){
        int now = 0,i = l+1;

        do{
            switch (s[i]){
                case '[':{
                    now++;
                    break;
                }
                case ']':{
                    now--;
                    break;
                }
            }
            i++;
        }while (now!=0);
//      now==0;
        while (s[i]!=',') i++;
//      s[i]==','
        dfs(l+1,i-1,dep+1),dfs(i+1,r-1,dep+1);          
    }else{
        int x = 0;
        for (int i = l;i <= r;i++) x = x*10 + s[i]-'0';
        mmap[dep][x]++;
        mmap2[x].push_back(dep);
        total++;
    }
}   

int main(){
    #ifdef LOCAL_DEFINE
        freopen("F:\\c++source\\rush_in.txt", "r", stdin);
    #endif
    ios::sync_with_stdio(0),cin.tie(0);
    int T;
    cin >> T;
    while (T--){
        for(int i = 0;i<=18;i++) mmap[i].clear();
        mmap2.clear();
        total = 0;          
        cin >> s;
        n = s.size();
        int ans = -1;
        if (s[0]!='['){
            ans = 0;
        }else{
            dfs(0,n-1,0);
            for (auto temp:mmap2){
                long long x = temp.first;
                auto v = temp.second;
                for (auto dep:v){
                    int tempans = 0;
                    int now = dep;
                    long long xx = x;
                    while (xx<=(long long) 1e10 && now >= 0){
                        tempans += mmap[now][xx];
                        now--;
                        xx*=2;                      
                    }
                    xx = x;
                    now = dep;
                    while (xx%2==0 && now <= 18){
                        xx/=2;
                        now++;                      
                        tempans += mmap[now][xx];
                    }
                    if (ans==-1){
                        ans = total - tempans;
                    }else{
                        ans = min(ans,total-tempans);
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/7878423.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值