UVA 12166 Equilibrium Mobile

直接贪心嘛。先想想最后平衡的时候,如果知道了总重量,那么每一个结点的重量其实也就确定了。

每个结点在左在右其实都不影响,只和层数有关。现在反过来,如果不修改某个结点,那么就可以计算出总质量,取总质量出现次数最多的保持不变。

/*********************************************************
*      --------------Tyrannosaurus---------              *
*   author AbyssalFish                                   *
**********************************************************/
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int maxd = 16, LEN = (1<<15)*12 + 10;
char s[LEN];

map<ll,int> cnt;

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    int T; scanf("%d",&T);
    while(T--){
        scanf("%s",s);
        char *p = s;
        int d = 0;
        cnt.clear();
        while(*p){
            if(isdigit(*p)){
                ll x = *p-‘0‘;
                while(isdigit(*(++p))) x = x*10+*p-‘0‘;
                cnt[x<<d]++;
                if(*p == ‘]‘) d--;
                if(!*p) break;
            }
            else if(*p == ‘[‘) d++;
            p++;
        }
        int sum = 0, M = 0;
        for(auto pr: cnt){
            sum += pr.second;
            M = max(M,pr.second);
        }
        printf("%d\n", sum-M);
    }
    return 0;
}
时间: 2024-08-18 10:54:28

UVA 12166 Equilibrium Mobile的相关文章

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

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

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

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 ea

UVa 12166(字符型二叉树的dfs)

一.题目 [PDF Link] 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 o

UVa 12166 修改天平

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3318 题意:给一个深度不超过16的二叉树,代表一个天平.每根杆悬挂在中间,每个秤砣的重量已知,至少修改多少个秤砣的重量才能让天平平衡? 要让天平平衡,必须以其中一个秤砣作为标准,然后修改其余的秤砣.当以深度为d,值为x的叶子节点作为标准时,可以发现此时天平的总质量为x<<d.

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)的推导,结点的深度和

uva 12166 bfs

这一题与白书上的一题关于天平的题目有些相似 uva839 #include <iostream> #include <cstdio> #include <cstring> using namespace std; bool solve(int& w){ int w1,w2,d1,d2; cin >> w1 >> d1 >> w2 >> d2; bool b1 = true,b2 = true; if(!w1) b1

Uva 12569 Planning mobile robot on Tree (EASY Version)

基本思路就是Bfs: 本题的一个关键就是如何判段状态重复. 1.如果将状态用一个int型数组表示,即假设为int state[17],state[0]代表机器人的位置,从1到M从小到大表示障碍物的位置.那么如果直接用STL中的set是会超时的,但如果自己建立一个hash方法,像这样: int getKey(State& s) { long long v = 0; for(int i=0; i<=M; ++i ) { v = v * 10 + s[i]; } return v % hashSi