Codeforces Round #195 (Div. 2) C--Vasily the Bear and Sequence(贪心)

本文介绍了一道关于序列的算法题目,目标是找出序列中的一组数,使得这些数的美度(即能被2的最大幂次整除)最大化。通过贪心算法结合位运算的方法,实现了高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Vasily the Bear and Sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasily the bear has got a sequence of positive integers a1, a2, ..., an. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum.

The beauty of the written out numbers b1, b2, ..., bk is such maximum non-negative integer v, that number b1 and b2 and ... and bk is divisible by number 2v without a remainder. If such number v doesn't exist (that is, for any non-negative integer v, number b1 and b2and ... and bk is divisible by 2v without a remainder), the beauty of the written out numbers equals -1.

Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible.

Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by "&", in Pascal — by "and".

Input

The first line contains integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ a1 < a2 < ... < an ≤ 109).

Output

In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b1, b2, ..., bk— the numbers to write out. You are allowed to print numbers b1, b2, ..., bk in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them.

Sample test(s)
input
5
1 2 3 4 5
output
2
4 5
input
3
1 2 4
output
1
4





思路:  这是一道贪心题,基于位运算
    我们要使结尾的0尽可能多,首先要保证所有0的前一位是1;
    由于10^9<2^30,所以最多有29个0
    我们不妨讨论1的位置:
    要使第1~v位都是0,那么就要保证第v+1位是1;
    所以我们暴力查找第v+1位是1的数,将他们&运算,检验后v为是否都是0
    符合就输出,不符合就再查找第v位是1的数。。。。。。


代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#define maxn 100005
using namespace std;
int a[maxn];
vector<int>p;
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    for(int i=30;i>=0;i--)
    {
        p.clear();
        int tem=-1;
        for(int j=0;j<n;j++)
            if(a[j]&(1<<i))
            {
                p.push_back(a[j]);
                if(tem==-1)tem=a[j];
                else tem&=a[j];
            }
        if(tem%(1<<i)==0)break;
    }
    printf("%d\n",p.size());
    for(int i=0;i<p.size()-1;i++)
        printf("%d ",p[i]);
    printf("%d\n",p[p.size()-1]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值