zoukankan      html  css  js  c++  java
  • 879C

    贪心

    题目看错了。。。还以为是从操作序列中选5个。。。然后半个小时没了。。。

    我们把每位分别用0和1带入,看看返回值是什么,然后分类讨论。千万不用特判!!!之前忘了删了就fst。。。

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 5e5 + 5;
    int n, x1, x2, x3;
    int a[N], mark[N];
    char s[N][10];
    int calc(int x, int t)
    {
        t = (t << x);
        for(int i = 1; i <= n; ++i) 
        {
            if(s[i][0] == '|') t |= a[i];
            if(s[i][0] == '&') t &= a[i];
            if(s[i][0] == '^') t ^= a[i]; 
        }
        return (t & (1 << x)) > 0;
    }
    int main()
    {
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i)
        {
            int x;
            scanf("%s%d", s[i], &a[i]);
            for(int j = 0; j < 10; ++j) if(a[i] & (1 << j)) mark[j] = 1;
        }
        x2 = 1023;
        for(int i = 0; i < 10; ++i) 
        {
            int x = calc(i, 0), y = calc(i, 1);
        //        printf("i = %d x = %d y = %d
    ", i, x, y);
            if(x == 0 && y == 0) x2 -= (1 << i);
            if(x == 1 && y == 0) x3 += (1 << i);
    //            if(x == 0 && y == 1) x2 += (1 << i);
            if(x == 1 && y == 1) x1 += (1 << i);
        } 
        int cnt = 3;
        if(!x1) --cnt;
    //    if(!x2) --cnt;
        if(!x3) --cnt;
        printf("%d
    ", cnt);
        if(x1) printf("| %d
    ", x1);
        printf("& %d
    ", x2);
        if(x3) printf("^ %d
    ", x3);
        return 0;
    }
    View Code
  • 相关阅读:
    Java类与对象
    读《大道至简——团队缺乏的不只是管理》有感
    java课后作业
    c++ 创建单项链表
    c++ 结构指针和双向链表
    c++ 自定义数据结构运用
    c++ 时间函数和结构化数据
    c++ 结束程序的几种方式
    c++ main函数的参数
    c++ 参数个数可变的函数
  • 原文地址:https://www.cnblogs.com/19992147orz/p/7745146.html
Copyright © 2011-2022 走看看