【NOIP模拟题】“与”(位运算)
生活随笔
收集整理的這篇文章主要介紹了
【NOIP模拟题】“与”(位运算)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
因為是與運算,所以我們可以貪心地每次找最高位的,將他們加入到新的序列中,然后每一次在這個新的序列繼續找下一個位。
然后最后序列中任意兩個的與運算的值都是一樣的且是最大的。
#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 << endl inline 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; }
?
?
?
?
題目描述:
給你一個長度為n的序列A,請你求出一對Ai,Aj(1<=i<j<=n)使Ai“與”Aj最大。
Ps:“與”表示位運算and,在c++中表示為&。
輸入描述:
第一行為n。接下來n行,一行一個數字表示Ai。
輸出描述:
輸出最大的Ai“與”Aj的結果。
樣例輸入:
3
8
10
2
樣例輸出:
8
樣例解釋:
8?and?10?=?8
8?and?2?=?0
10?and?2?=?2
數據范圍:
20%的數據保證n<=5000
100%的數據保證?n<=3*10^5,0<=Ai<=10^9
總結
以上是生活随笔為你收集整理的【NOIP模拟题】“与”(位运算)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unix时间相关的函数
- 下一篇: Java的设计模式----strateg