[Noip simulation question] "and" (bitwise Operation)

Source: Internet
Author: User

Because it is an operation, we can greedy to find the highest bit each time, add them to the new sequence, and then find the next bit each time in the new sequence.

Then, any two values in the final sequence are the same and the calculated values are the largest.

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <vector>#include <map>using namespace std;typedef long long ll;#define pii pair<int, int>#define mkpii make_pair<int, int>#define pdi pair<double, int>#define mkpdi make_pair<double, int>#define pli pair<ll, int>#define mkpli make_pair<ll, int>#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define error(x) (!(x)?puts("error"):0)#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endlinline const ll getint() { ll r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=3*10e5;int n, a[N], b[N];int main() {read(n);for1(i, 1, n) read(a[i]);for3(k, 31, 0) {int top=0;for1(i, 1, n) if(a[i]&(1<<k)) b[++top]=a[i];if(top>=2) {for1(i, 1, top) a[i]=b[i];n=top;}}printf("%d\n", a[1]&a[n]);return 0;}

  

 

 

 

 

Description:

Here is a sequence a with the length of N. Please find a pair of AI, AJ (1 <= I <j <= N) to make the ai "and" AJ "the largest.

PS: "and" indicates the bitwise operation and, in C ++, it indicates &.

Input description:

First Act n. In the next n rows, a row of numbers indicates AI.

Output description:

Output the largest AI and AJ results.

Sample input:

3

8

10

2

Sample output:

8

Example:

8 and 10 = 8

8 and 2 = 0

10 and 2 = 2

Data range:

20% data guarantee n <= 5000

100% data guarantee n <= 3*10 ^ 5, 0 <= AI <= 10 ^ 9

[Noip simulation question] "and" (bitwise Operation)

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.