码迷,mamicode.com
首页 > 其他好文 > 详细

879C

时间:2017-10-27 21:32:39      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:贪心   logs   char   opened   none   c++   space   color   alt   

贪心

题目看错了。。。还以为是从操作序列中选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\n", 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\n", cnt);
    if(x1) printf("| %d\n", x1);
    printf("& %d\n", x2);
    if(x3) printf("^ %d\n", x3);
    return 0;
}
View Code

 

879C

标签:贪心   logs   char   opened   none   c++   space   color   alt   

原文地址:http://www.cnblogs.com/19992147orz/p/7745146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!