D. XOR-gun (thinking + prefix XOR + violence)

https://codeforces.com/contest/1457/problem/D


By the way, this D hacked 400+ people after eating.

Idea: I started to see that the answer was only -1, 1, 2, but it was not.

The structure finds that if the highest 1 position of three consecutive bits is the same, then the goal can be achieved once.

So what is the limit in this case?

0001

0010

0011

01??

01??

1???

1???

It can be seen that in the case of the maximum value of 1e9, when there is no conflict in the sequence, there are 60 numbers. The rest will definitely be possible once.

Then the topic is pruned to below 60.

Consider two situations: one is the peak or trough of a range is directly XORed. (n^2)

The other is the case where two consecutive intervals are XORed separately and then merged. (n^3)

 

Set the boundary a[n+1]=1e18 on the code;

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=100;
typedef long long LL;
LL a[maxn];
LL sum[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  if(n>60){
    cout<<"1"<<endl;
    return 0;
  }
  a[n+1]=1e18;
  for(LL i=1;i<=n;i++) cin>>a[i];
  for(LL i=1;i<=n;i++){
    sum[i]=sum[i-1]^a[i];
  }
  LL res=1e18;
  for(LL l=1;l<=n;l++){
    for(LL r=l+1;r<=n;r++){
        LL t=sum[r]^sum[l-1];
        if(t>a[r+1]||t<a[l-1]){
            res=min(res,r-l+1-1);
          ///  debug(res);
        }
    }
  }
  for(LL i=1;i<=n;i++){///[i,j][j+1,k]
    for(LL j=i+1;j<=n;j++){
        for(LL k=j+1;k<=n;k++){
            LL t1=sum[j]^sum[i-1];
            LL t2=sum[k]^sum[j];
            if(t1>t2){
                res=min(res,k-j+1-1+j-i+1-1-1);
            }
        }
    }
  }
  if(res==1e18){
    cout<<"-1"<<endl;
  }
  else cout<<res<<endl;
return 0;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/110404960
XOR