B. Xor of 3 题解(思维+构造)

题目链接

题目思路

官方题解写的很好

但基本想不到。。。还是写个博客记录下

代码

#include<bits/stdc++.h>
#define fi first
#define se second
#define debug cout<<"I AM HERE"<<endl;
using namespace std;
typedef long long ll;
const int maxn=4e5+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-6;
int n;
int a[maxn];
vector<int> vec;
void solve(int l,int r){
    if(r-l==0) return ;
    for(int i=l;i+2<=r;i+=2){
        vec.push_back(i);
    }
    for(int i=r-4;i>=l;i-=2){
        vec.push_back(i);
    }
}
signed main(){
    int _;cin>>_;
    while(_--){
        vec.clear();
        cin>>n;
        int x=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            x^=a[i];
        }
        if(x==1){
            cout<<"NO\n";
            continue;
        }
        if(n%2==1){
            cout<<"YES\n";
            solve(1,n);
            cout<<vec.size()<<'\n';
            for(auto x:vec){
                cout<<x<<' ';
            }
            if(vec.size())
                cout<<'\n';
        }else{
            x=0;
            for(int i=1;i<=n;i++){
                x^=a[i];
                if(i%2==1&&x==0){
                    cout<<"YES\n";
                    solve(1,i);
                    solve(i+1,n);
                    cout<<vec.size()<<'\n';
                    for(auto x:vec){
                        cout<<x<<' ';
                    }
                    if(vec.size())
                        cout<<'\n';
                    break;
                }
                if(i==n){
                    cout<<"NO\n";
                }
            }
        }
    }
    return 0;
}


上一篇:LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal


下一篇:最短路算法总结