Codeforces 336C Vasily the Bear and Sequence 暴力

题目大意:

就是现在给出一个严格单调递增的数列a, 1 <= a1 < a2 < a3 ... < an <= 10^9 (1 <= n <= 10^5)

现在要求从中选出一些数作为b1, b2, b3... bm (m为选出的数的个数)

使得 b1 & b2 & b3 ... & bm的二进制末尾0的数量最大, 在满足这个值最大的同时如果有多个答案, 输出使得m尽量大的解, 如果依旧有多组解输出任意一组


大致思路:

要使得b1 & b2 & b3... & bm末尾0的数量最大由于最多也不会超过29个 10 ^9 < 2^29

所以可以枚举最终结果末尾0的个数为i, 把所有末尾第 i + 1 是1的数做&操作使得取得数的数量最多, 然后判断得到的数是否末尾有i个0, 是的话更新答案


代码如下:

Result  :  Accepted     Memory  :  400 KB     Time  :  62 ms

/*
 * Author: Gatevin
 * Created Time:  2015/2/27 17:34:19
 * File Name: poi~.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

int n;
int a[100010];

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for(int i = 29; i >= 0; i--)
    {
        int tmp = (1 << (i + 1)) - 1;
        int cnt = 0;
        for(int j = 1; j <= n; j++)
            if(a[j] & (1 << i))
            {
                cnt++;
                tmp &= a[j];
            }
        if(tmp % (1 << i) == 0)
        {
            cout<<cnt<<endl;
            for(int j = 1; j <= n; j++)
                if(a[j] & (1 << i))
                    printf("%d ", a[j]);
            return 0;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值