首页 > 其他分享 >Educational Codeforces Round 157 (Rated for Div. 2) D. XOR Construction

Educational Codeforces Round 157 (Rated for Div. 2) D. XOR Construction

时间:2023-11-04 12:11:05浏览次数:35  
标签:Educational Rated XOR int sum2 sum1 异或 le oplus

题目链接

题意
给你 \(n-1\) 个整数 \(a_1, a_2, \dots, a_{n-1}\) 。

你的任务是构造一个数组 \(b_1, b_2, \dots, b_n\) ,使得:

  • 从 \(0\) 到 \(n-1\) 的每个整数都在 \(b\) 中出现一次;
  • 对于从 \(1\) 到 \(n-1\) 的每个 \(i\) , \(b_i \oplus b_{i+1} = a_i\) (其中 \(\oplus\) 表示整数 \(b_i \oplus b_{i+1} = a_i\) 。(其中 \(\oplus\) 表示位向 XOR 运算符)。

输入

第一行包含一个整数 \(n\) ( \(2 \le n \le 2 \cdot 10^5\) )。

第二行包含 \(n-1\) 个整数 \(a_1, a_2, \dots, a_{n-1}\) ( \(0 \le a_i \le 2n\) )。

输入的其他限制条件:从给定的序列 \(a\) 中总是可以构造出至少一个有效的数组 \(b\) 。

思路

一般来说,区间异或的题目往往和前缀有关,所以我们不妨将公式进行罗列

\[\begin{aligned} b_1 \oplus b_2 &= a_1\\ b_2 \oplus b_3 &= a_2\\ \cdots\\ b_j \oplus b_{j + 1} &= a_j\\ \end{aligned} \]

把左右两边都异或起来可以推出:\(b_1 \oplus b_{j + 1} = \bigoplus\limits_{i = 1}^{j}a_i\)。

记前缀异或和 \(sum_x = \bigoplus\limits_{i = 1}^{x}a_i\)。

那么 \(b_{j + 1} = b_1 \oplus sum_{j}\),那么实际上是只要确定了 \(b_1\) 就可以算出所有的 \(b_j\)。

题目中要求要让 \(b\) 在 \(0\sim n - 1\) 之内,这是\(b\)数组和的最小状态

所以这实际上实在寻找一个 \(b_1\) 使得异或出来的所有\(b_i\)值和越小越好。

因为\(b_i\)和\(a_i\)和\(b_1\)有关,在这里\(a\)数组我们无法改变,为了让\(b_i\)减小,只能靠修改\(b_1\)

让每一位最小化,自然最后整体就会最小化,这就是局部最优达成全局最优,贪心的思路。

所以我们对于每一位进行考虑,假设所有\(a_i\)的第 \(i\) 位为 \(1\) 的个数有\(sum1\)个,第\(i\)位为 \(0\) 的个数有\(sum2\)个。

如果说\(sum2 < sum1\)

那我们最好让\(b_1\)异或上一个 \(2^i\),这样可以让第\(i\)位为\(0\)的\(b_i\)的个数变成\(sum1\),为\(1\)的个数变为\(sum2\)

从而达到让\(b_i\)整体变小的结果。

代码

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+20;
int n,a[N],b[N];
inline void init()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1; i<n; i++)
	{
		cin>>a[i];
		a[i]^=a[i-1];//前缀异或和
	}
	for(int i=0; i<30; i++)//枚举每一位 
	{
		int sum1=0,sum2=0;
		for(int j=1; j<n; j++)
			if (a[j]>>i & 1)
				sum1++;
			else sum2++;
		if (sum1>sum2)//这样可以减小整体该位为1的个数
			b[1]+=(1<<i);
	}
	for(int i=2; i<=n; i++)
		b[i]=a[i-1]^b[1];//根据上述公式
	for(int i=1; i<=n; i++)
		cout<<b[i]<<" ";
}
signed main()
{
	init();
	return 0;
}

标签:Educational,Rated,XOR,int,sum2,sum1,异或,le,oplus
From: https://www.cnblogs.com/gzh-red/p/17809170.html

相关文章

  • [ARC122D] XOR Game
    ProblemStatementThereare$2N$integerswrittenonablackboard.The$i$-thintegeris$A_i$.AliceandBobwillplayagameconsistingof$N$rounds.Ineachround,theydothefollowing:First,Alicechoosesanintegerontheblackboardanderasesit.......
  • How to format lists in pandoc-generated docx documents?
    Sorry,thelistindentationsarecurrentlyhard-codedandcan'tbecustomized.Youcould,however,postprocessthedocxproducedbypandoc,changingthefilenumbering.xmlinthedocxcontainer.Oryoucouldmodifythesourcecodeandrecompile.Thes......
  • Educational Codeforces Round 134 (Div.2) D 题解
    题目链接D.MaximumAND题目大意给定两组序列\(a\)\(b\),长度为\(n\),现有一新序列\(c\),长度也为\(n\)。其中,\(c_i=a_i\oplusb_i\)。定义\(f(a,b)=c_1\&c_2\&……\&c_n\)。现在你可以随意编排\(b\)序列的顺序,求\(f(a,b)\)的最大值。思路以下位运算均是......
  • Educational Codeforces Round 128 (Rated for Div. 2)
    添加链接描述C题显然二分0的数量,然后双指针,算一下前缀和后缀1的数量即可。#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<map>#include<vector>#include<set>#defineAputs("YES")#defineBputs("NO&q......
  • mysql数据库管理-FEDERATED存储引擎远程链接MYSQL
    开启FEDERATED存储引擎1.1、查看存储引擎存在的FEDERATED存储引擎就配置文件开启不存在就安装查看showengines;YES支持并开启DEFAULT支持并开启,并且为默认引擎;NO不支持;DISABLED支持,但未开启。创建federated引擎表创建语句最好和原表语句一样,当然去掉id的auto之类的。CREATE......
  • XOR 加密
    1.代码#include<stdio.h>#include<stdlib.h>#include<time.h>intmain(){srand(time(NULL));inta,b,c,i,n;longlongd=0;printf("原文:");scanf("%d",&a);printf("密钥长度:");sc......
  • BLOXORZ
    1关密码:780464→↓↓→→↓→2关密码:290299↑→↓→→→→↑↑↓↓→→→→↑←↑3关密码:918660→↑→→→↑→↓←↑↑→↓←←↑→→→→↓↓↓→↑4关密码:520967↑←↑→→↑→→→→→→↓→↓↓↓↓↓→↑←←←←←←↓5关密码:028431......
  • Educational Codeforces Round 144(CF1796 D~E) 题解
    D.MaximumSubarray题目链接十分钟秒了一道*2000,非常爽,但是个人感觉确实非常简单。下面令\(b_i=a_{i}-x\),\(b_i'=a_i+x\)。因为\(k<=20\),因此考虑一个复杂度与\(k\)相关的做法。不妨dp:设\(f_{i,j,0/1}\)表示考虑了前\(i\)个数,对\(j\)个数加上了\(x\),第\(i\)......
  • Educational Codeforces Round 145 (Rated for Div. 2) B. Points on Plane
    给一个二维平面,你需要在上面放\(n\)个芯片。将一个芯片放在\((x,y)\)的代价为\(|x|+|y|\)。放\(n\)个代价的代价为,所有芯片中耗费的最大代价。并且你需要保证任意两个芯片之间的距离严格\(>1\)。现在给出\(n\)给芯片,询问放\(n\)个芯片的最小代价。一:不难想到......
  • golang之xorm简单使用
    gogetgithub.com/go-xorm/xormpackagemainimport("fmt"_"github.com/go-sql-driver/mysql""github.com/go-xorm/xorm")typePointInfostruct{Idint64`xorm:"pkautoincr"`Product......