Codeforces Round #743 (Div. 2) D. Xor of 3 模拟 + 构造

传送门

文章目录

题意:

给你一个 01 01 01序列 a a a,定义一次操作是选择一个 [ 1 , n − 2 ] [1,n-2] [1,n2]范围内的下表,将 a i , a i + 1 , a i + 2 a_i,a_{i+1},a_{i+2} ai,ai+1,ai+2都变成 a i ⊕ a i + 1 ⊕ a i + 2 a_i\oplus a_{i+1} \oplus a_{i+2} aiai+1ai+2,问你能否在不超过 n n n次操作的前提下,将序列 a a a全部变成 0 0 0

3 ≤ n ≤ 2 e 5 3\le n\le 2e5 3n2e5

思路:

首先注意到,这个操作不会改变整个数列和的奇偶性,所以初始为奇数的时候肯定无解,并且一定至少一个 0 0 0

我们考虑一种比较直白点的贪心,找到一个 0 0 0的位置,当他左边有偶数个 1 1 1的时候,显然可以将其全部变成 0 0 0,操作的次数为 c n t 2 \frac{cnt}{2} 2cnt,其中 c n t cnt cnt 1 1 1的个数。当有奇数个 1 1 1的时候,需要分两种情况:

( 1 ) (1) (1) 当右边的第一个数为 1 1 1的时候,显然可以操作 [ 1 , 0 , 1 ] [1,0,1] [1,0,1]这个区间,将其全部变为 0 0 0,让后左边只有偶数个 1 1 1了,就可以将其全部变为 0 0 0

( 2 ) (2) (2) 当右边第一个数为 0 0 0的时候,操作 [ 1 , 0 , 0 ] [1,0,0] [1,0,0]这个区间将其全部变为 1 1 1

注意到经过以上操作,我们可以将这个序列变成一个 [ 0 , 0 , . . . , 0 , 1 , 1 , . . . , 1 ] [0,0,...,0,1,1,...,1] [0,0,...,0,1,1,...,1]形式的序列,这个时候还需要检查一下是否全部变成 1 1 1了,否则显然可以选择 [ 0 , 1 , 1 ] [0,1,1] [0,1,1]这样的区间将其变为 [ 0 , 0 , 0 ] [0,0,0] [0,0,0]

操作次数还是比较少的,能卡到上界的就是 ( 2 ) (2) (2)这个情况,加上最后将其变成 [ 0 , 0 , 0 ] [0,0,0] [0,0,0]的操作,上界是 n n n

// Problem: D. Xor of 3
// Contest: Codeforces - Codeforces Round #743 (Div. 2)
// URL: https://codeforces.com/contest/1573/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n;
int a[N];

bool check() {
	int sum=0;
	for(int i=1;i<=n;i++) sum+=a[i];
	if(sum%2||sum==n) return false;
	vector<int>ans;
	a[n+1]=-1; a[0]=-1;
	int pre=-1;
	for(int i=1;i<=n;i++) {
		if(a[i]==0) {
			if(i==1) continue;
			int cnt=0,st=i;
			// while(st-1>=1&&a[st-1]==1) cnt++,st--;
			if(pre!=-1) cnt=i-pre;
			else cnt=0;
			if(cnt%2) {
				if(a[i+1]==1) {
					ans.pb(i-1); cnt--;
					a[i+1]=0; i++; 
					a[i-2]=0; st=i-4;
					while(cnt) ans.pb(st),a[st]=a[st+1]=0,st-=2,cnt-=2;
					pre=-1;
				} else {
					ans.pb(i-1); a[i]=1; a[i+1]=1; i++;
				}
			} else {
				st=i-2;
				while(cnt) ans.pb(st),a[st]=a[st+1]=0,st-=2,cnt-=2;
				pre=-1;
			}
			
		} else {
			if(pre==-1) pre=i;
		}
	}
	sum=0;
	for(int i=1;i<=n;i++) sum+=a[i];
	if(sum==n) return false;
	puts("YES");
	int st=1;
	while(st<=n&&a[st]!=1) st++;
	if(st<=n) {
		st--;
		for(int i=st;i<=n-2;i+=2) ans.pb(i);
	}
	printf("%d\n",ans.size());
	for(auto x:ans) printf("%d ",x); puts("");
	return true;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	
	int _; scanf("%d",&_);
	while(_--) {
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		if(!check()) puts("NO");
	}



	return 0;
}
/*

*/









  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值