题解 | #2024#

2024

https://ac.nowcoder.com/acm/contest/72445/A

//按位与.用的灵神教的贪心
#include<bits/stdc++.h>
using namespace std;
#define int long long 
const int n=1e6+11;
int a,b[n],c,d,e;
signed main()
{
    ios::sync_with_stdio(false);
      cin.tie(0),cout.tie(0);
    cin>>a;
    for(int i=1;i<=a;i++)
    {
        cin>>b[i];
    }
    int an=0,w=0;
    vector<int>t;
    for(int i=30;i>=0;i--)
    {
        an+=pow(2,i);
        w+=pow(2,i);
        int f=0;
        for(int j=1;j<=a;j++)
        {
            int h=b[j];
            h&=w;
            int rr=h&an;
            if(rr==an)
            {
                f++;
            }
        }
        if(f<2)
        {
            an-=pow(2,i);
        }
        else if(f>=2)
        {
            t.clear();
            for(int j=1;j<=a;j++)
            {
                int h=b[j];
                h&=w;
                int rr=h&an;
                if(rr==an)
                {
                    t.push_back(j);      
                }
            }
        }
    }
    if(t.size()<2)
    {
        cout<<"1"<<" "<<"2"<<endl;
    }
    else
    {
        for(int i=0;i<2;i++)
        {
            cout<<t[i]<<" ";
        }
    }
}
//2154656

全部评论

相关推荐

2 收藏 评论
分享
牛客网
牛客企业服务