题解:CF1572B Xor of 3

· · 题解

好题。

操作比较奇怪,我们在前缀异或序列上考虑问题,不难发现操作相当于 pre_i \gets pre_{i+2},pre_{i+1} \gets pre_{i-1}。

奇数位置偶数位置相对独立,然后你发现偶数位置的 pre 可以从 pre_0 处借来一个 0,所以不妨先考虑处理奇数位置,最后再操作 1,3,5,\dots 来将偶数位置全部变成 0。

显然奇数位置必须有一个 0,然后就可以通过操作来将这个 0 向前或者向后移动了。

注意到 pre_n 没法改变,所以必须是 0,这个特判。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+114;
int a[maxn];
int n;
void work(){
    cin>>n;
    int st=1;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        a[i]^=a[i-1];
        if(i%2==1) st&=a[i];
    }
    if(a[n]==1){
        cout<<"NO\n";
        return ;
    }
    if(st==1){
        cout<<"NO\n";
        return ;
    }
    cout<<"YES\n";
    vector<int> opt;
    int key=1;
    for(int i=1;i<=n;i++){
        if(i%2==1&&a[i]==0) key=i;
    }
    for(int i=key-2;i>=1;i-=2){
        if(a[i]==1) opt.push_back(i),a[i]=0;
    }
    for(int i=key+2;i+1<=n;i+=2){
        if(a[i]==1) opt.push_back(i-1),a[i]=0;
    }
    for(int i=1;i+2<=n;i+=2){
        opt.push_back(i);
    }
    cout<<opt.size()<<'\n';
    for(int x:opt) cout<<x<<" ";
    cout<<'\n';
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--) work();
    return 0;
}