本文主要是介绍@UPC @2018江苏冬令营3 B: 庭师的利刃,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目描述
作为白玉楼的庭师,妖梦虽然不会n刀流,但是却领悟了生命二刀流。然而我也是个剑的收藏者,家里屯着n把剑,每一把剑都有一个灵魂值a[i],由于一些剑之间可能有共鸣,所以我需要两把契合度最高的剑。据妖梦所说,两把编号为i,j剑的契合度为a[i] and a[j]。如何深得剑的灵魂呢?(即求最大值)
输入
第一行一个整数n,代表藏剑数。
第二行n个整数,第i个整数表示a[i]。
输出
输出包含一个正整数,最好的两把剑的契合度。
样例输入
5 12 5 6 3 1
样例输出
4
提示
对于40%的数据 n ≤ 1,000
对于100%的数据 n ≤ 1,000,000,0 ≤ a[i] < 2^31
[思路]
用二进制形式表示,从最高位开始,尽可能的保证最高位1 的个数相同, 向下更新
代码
#include <bits/stdc++.h>
#include <stdlib.h>
#include <utility>
#define findx(x,b,n) lower_bound(b+1,b+1+n,x)-b
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define SHUT ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r#pragma comment(linker, "/STACK:1024000000,1024000000") // 扩栈
//next_permutation(a+1,a+x) 全排列
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())using namespace std;typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;const double PI=acos(-1.0);
const ll INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};inline void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);};}
inline ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
inline ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}inline ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
inline ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}/*********************************head************************//** Author : siz*/VI v1;
int a[maxn];
int main()
{int n;scanf("%d",&n);rep(i,0,n){scanf("%d",&a[i]);}ll ans = 0;per(i,0,31){int cont = 0;ll dig = 1ll<<i;rep(j,0,n){if( ( (ans&a[j])==ans) && ( (a[j]&dig)==dig))cont++;if( cont >=2)break;}if( cont>=2 )ans += dig;}printf("%lld\n",ans);return 0;
}
这篇关于@UPC @2018江苏冬令营3 B: 庭师的利刃的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!