• 1483 化学变换 乱搞题


    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1483

    首先,要知道在0---4e5内的体积下,一定存在答案。不会存在一些更大的答案。(要证明我也不晓得,我直觉)

    那么我的思路就是,枚举,0--4e5,然后需要O(1)判断和更新答案。

    那么预处理isok[cnt]表示产生cnt这个数字,其中有两个参数,has,表示一共有多少个数能产生这个数字。那么明显has需要等于n,才是答案的贡献。然后step,表示这n个数产生cnt这个数字,需要的步数的总和。这个可以用dfs维护。每次要么 * 2,要么 / 2,产生相同的数字,就可以return了、最优的复杂度是偶数的情况,是logn。奇数的话,也就多了几个常数,因为 / 2多次(应该不超过3次),都会变成偶数

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <assert.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <bitset>
    const int maxn = 1e5 + 20;
    int mx;
    struct node {
        int step;
        int has;
    }isok[maxn * 4];
    int vis[maxn * 4];
    int DFN;
    void calc(int x, int cnt) {
        if (x >= mx) return;
        if (vis[x] == DFN) return ;
        vis[x] = DFN;
        isok[x].step += cnt;
        isok[x].has++;
        calc(x * 2, cnt + 1);
        calc(x / 2, cnt + 1);
    }
    void work() {
        int n;
        scanf("%d", &n);
        mx = 4e5;
        for (int i = 1; i <= n; ++i) {
            int x;
            DFN++;
            scanf("%d", &x);
            calc(x, 0);
        }
        int ans = inf;
        for (int i = 0; i <= mx; ++i) {
            if (isok[i].has != n) continue;
            ans = min(ans, isok[i].step);
        }
        cout << ans << endl;
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        work();
        return 0;
    }
    View Code
  • 相关阅读:
    存储过程访问外部服务器
    MVC4 code first 增加属性,对应自动修改列的方法笔记
    摘抄
    一条命令使win7可以直接运行.net3.5程序
    工作心得
    删除一个不存在的东西可以抛出异常吗
    洛谷P3379 【模板】最近公共祖先(LCA)
    洛谷P1967 货车运输
    洛谷P1653 猴子
    洛谷P2278 [HNOI2003]操作系统
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6390284.html
Copyright © 2020-2023  润新知