zoukankan      html  css  js  c++  java
  • 【codeforces 779E】Bitwise Formula

    【题目链接】:http://codeforces.com/contest/779/problem/E

    【题意】

    给你n个长度为m的二进制数
    (有一些是通过位运算操作两个数的形式给出);
    然后有一个未知数字,用符号’?’表示;
    (所有的数字都是长度为m的二进制数);
    然后让你确定这个’?’使得n个数字的和最大、最小;

    【题解】

    模拟题;
    会有嵌套的情况;

    a=x & y
    b=a&?
    类似这样.
    所以在算b之前,先把a的值确定下来;
    明白了怎么算之后;
    枚举那个未知数字的m位的每一位;
    每一位都只有两种可能,即为0或者为1;
    假设为0;
    然后带进去算一下最后结果,这一位的和为多少;
    假设为1
    然后带进去算一下最后结果,这一位的和为多少;
    如果要最大值就选那个大的对应的数字,最小值则相反。

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define ps push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%lld",&x)
    #define ref(x) scanf("%lf",&x)
    
    typedef pair<int, int> pii;
    typedef pair<LL, LL> pll;
    
    const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
    const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
    const double pi = acos(-1.0);
    const int M = 1e3+100;
    const int N = 5e3 + 100;
    
    struct abc
    {
        int a, b, p;
        int sz[M];
    };
    
    int n, m,change,f[N];
    string name,s;
    map <string, int> dic;
    abc a[N];
    vector<int>v1, v2;
    
    int get_val(int pos)
    {
        int sum = 0;
        f[0] = change;
        rep1(i, 1, n)
        {
            if (a[i].p == 0)
            {
                sum += a[i].sz[pos];
                f[i] = a[i].sz[pos];
                continue;
            }
            int x = f[a[i].a], y = f[a[i].b];
            int p = a[i].p;
            if (p == 1)
                f[i] = x&y;
            if (p == 2)
                f[i] = x | y;
            if (p == 3)
                f[i] = x^y;
            sum += f[i];
        }
        return sum;
    }
    
    int main()
    {
        //freopen("F:\rush.txt", "r", stdin);
        rei(n), rei(m);
        dic["?"] = 0;
        rep1(i, 1, n)
        {
            cin >> name; dic[name] = i;
            cin >> s; cin >> s;
            if (s[0] == '1' || s[0] == '0')
            {
                a[i].p = 0;
                rep1(j, 1, m)
                    a[i].sz[j] = s[j - 1]-'0';
            }
            else
            {
                a[i].a = dic[s];
                cin >> s;
                if (s[0] == 'A') a[i].p = 1;
                if (s[0] == 'O') a[i].p = 2;
                if (s[0] == 'X') a[i].p = 3;
                cin >> s;
                a[i].b = dic[s];
            }
        }
        rep1(i, 1, m)
        {
            change = 0; int temp0 = get_val(i);
            change = 1; int temp1 = get_val(i);
            if (temp0 <= temp1)
                v1.ps(0);
            else
                v1.ps(1);
            if (temp0 >= temp1)
                v2.ps(0);
            else
                v2.ps(1);
        }
        rep1(i, 0, m - 1)
            printf("%d", v1[i]);
        puts("");
        rep1(i, 0, m - 1)
            printf("%d", v2[i]);
        puts("");
        //printf("
    %.2lf sec 
    ", (double)clock() / CLOCKS_PER_SEC);
        return 0;
    }
  • 相关阅读:
    第2章 创建基础框架
    目录
    工厂方法模式(Factory Method)
    petshop4.0 详解之七(PetShop表示层设计)
    第1章 启动电子商务网站
    第3章 创建商品目录:第Ⅰ部分
    编写一个JAVA应用程序,用户从键盘只能输入整数,程序输出这些整数的乘积
    书上例题练习第一章
    java与C/C++的区别
    安装JDK遇到的问题
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626495.html
Copyright © 2011-2022 走看看