CF 336C (Vasily the Bear and Sequence-greedy-not monotonous)

Source: Internet
Author: User

C. Vasily the Bear and Sequence
Time limit per test2 seconds
Memory limit per test256 megabytes
Inputstandard input
Outputstandard output
Vasily the bear has got a sequence of positive integers a1, clerk a2, clerk ,..., your. 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, describ2 ,...,   bk is such maximum non-negative integer v, that number b1 and b2 and... and bk is divisible by number 2 v 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 2 v without a remainder), the beauty of the written out numbers equals-1.

Tell the bear which numbers he shocould 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 your numbers as possible.

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

Input
The first line contains integer n (1 rows ≤ limit n rows ≤ limit 105 ). the second line contains n space-separated integers a1, clerk a2, clerk ,..., when an (1 hour ≤ faster a1 hour <faster a2 hour <... latency <cost an ≤ cost 109 ).

Output
In the first line print a single integer k (k digit> limit 0), showing how many numbers to write out. in the second line print k integers b1, b2, numbers ,...,   bk-the numbers to write out. you are allowed to print numbers b1, clerk b2, clerk ,...,   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

 

I know everything from big to small.

It is worth mentioning that this does not satisfy the monotonicity.

 

#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<ctime>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define MAXN (100000+10)long long mul(long long a,long long b){return (a*b)%F;}long long add(long long a,long long b){return (a+b)%F;}long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}typedef long long ll;int n,a[MAXN],ans[MAXN];int main(){//freopen("seq.in","r",stdin);cin>>n;For(i,n) scanf("%d",&a[i]);sort(a+1,a+1+n);int v=1;while (v<a[n]) v<<=1;while (v){int tot=0;For(i,n) if (v&a[i]) ans[++tot]=a[i];if (!tot) {v>>=1;continue;}int t=ans[1];Fork(i,2,tot) t&=ans[i];if (t%v==0){cout<<tot<<endl;For(i,tot-1) cout<<ans[i]<<' ';cout<<ans[tot]<<endl;return 0;}v>>=1;}cout<<n<<endl;For(i,n-1) cout<<a[i]<<' ';cout<<a[n]<<endl;return 0;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.