首页 > 其他分享 >CF1054D Changing Array

CF1054D Changing Array

时间:2023-10-10 21:23:59浏览次数:37  
标签:xor int 异或 Changing Array include buf oplus CF1054D

题意

给定 \(n\) 个小于 \(2 ^ k\) 的数。

可以任意让若干数 \(xor\) \(2 ^ k - 1\)。

问使得最终区间 \(xor\) 不为 \(0\) 的最大个数。

Sol

考虑前缀异或和。

记异或和的数组为 \(s\)。

现在一个区间的贡献变为 \(s_r \oplus s_{l - 1}\)。

考虑何时该贡献为 \(0\)。

显然当 \(s_r = s_{l - 1}\) 时,贡献为 \(0\)。

此时考虑将 \(s_r \oplus\) 上一个 \(2 ^ k - 1\)。

我们不妨记 \(k' = 2 ^ k - 1\)

一个数 \(\oplus k'\) 最多出现另一个不同的数。

考虑贪心,每次对 \(s_i, s_i \oplus k'\) 取最小值。

可以证明此时的方案为最优。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <unordered_map>
#define int long long
using namespace std;

#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif

int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
template <typename _Tp>
void write(_Tp x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
const int N = 2e5 + 5;
unordered_map <int, int> mp;
array <int, N> s;
signed main() {
	int n = read(), k = read();
	k = (1 << k) - 1;
	for (int i = 1; i <= n; i++)
		s[i] = read(), s[i] ^= s[i - 1];
	long long ans = 0;
	mp[0]++;
	for (int i = 1; i <= n; i++) {
		if (mp[s[i] ^ k] < mp[s[i]])
			s[i] ^= k;
		ans += mp[s[i]];
		mp[s[i]]++;
	}
	/* puts(""); */
	write(n * (n + 1) / 2 - ans), puts("");
	return 0;
}

`

标签:xor,int,异或,Changing,Array,include,buf,oplus,CF1054D
From: https://www.cnblogs.com/cxqghzj/p/17755776.html

相关文章

  • ArrayList 介绍
       ......
  • [CF1158F]Density of subarrays
    F-Densityofsubarrays屲,平衡复杂度题首先考虑如何求一个序列的密度从最左端开始,找到需序列\(A[1...n]\)的最小段\(A[1...a_1]\),使其包含\(1\simc\)的所有颜色,然后又以\(a_1+1\)为起点,找下一个最短的包含\(1\simc\)的所有颜色的段,最后找到的这样的段的个数就是这个序列......
  • Educational Codeforces Round 152 (Div. 2) D. Array Painting(双指针)
    EducationalCodeforcesRound152(Div.2)D.ArrayPainting//思路:双指针找连续正数段//若段中出现2,则更新两头的0的情况,若为涂色则改为true//若无2,则优先更新左侧0,若左0已经为true,则更新右侧0//数组开头结尾特判#defineintlonglong#defineldlongdoubleusingnam......
  • 顺序容器(vector、deque、list、forward_list、array 、string)
    一、顺序容器概述   顺序容器提供了控制元素存储和访问顺序的能力,顺序与元素加入容器时的位置相对应。1、常见的顺序容器类型:vector:可变大小的数组。支持快速随机访问,在尾部之外的位置插入或者删除元素可能很慢。deque:双端队列。支持快速随机访问。在头尾位置插入/删除速度很......
  • 顺序容器(vector、deque、list、forward_list、array 、string)
    一、顺序容器概述   顺序容器提供了控制元素存储和访问顺序的能力,顺序与元素加入容器时的位置相对应。1、常见的顺序容器类型:vector:可变大小的数组。支持快速随机访问,在尾部之外的位置插入或者删除元素可能很慢。deque:双端队列。支持快速随机访问。在头尾位置插入/删除速度很快......
  • np.expand_dims: AxisError: axis 4 is out of bounds for array of dimension 4
    np.expand_dims axis=0时,[]加在最外面axis=1时,给每一行都加[]axis=2时,给每一个元素都加[]  x_train=np.expand_dims(X,axis=4)---------------------------------------------------------------------------AxisErrorTrac......
  • c: Pointer two-dimensional array
     printf("helloworld,c\n");printf("你好,中国\n");intduArry[]={0,1,2,3,4,5};int*pArr;pArr=duArry;pArr=&duArry[0];intl=sizeof(duArry)/sizeof(duArry[0]);for(inti=0;i<l;i++)......
  • Go - Sorting Arrays or Slices
    Problem: Youwanttosortelementsinanarrayorslice.Solution: Forint,float64,andstringarraysorslicesyoucanusesort.Ints,sort.Float64s,andsort.Strings.Youcanalsouseacustomcomparatorbyusingsort.Slice.Forstructs,youcan......
  • Go - Making Arrays and Slices Safe for Concurrent Use
    Problem: Youwanttomakearraysandslicessafeforconcurrentusebymultiplegoroutines.Solution: Useamutexfromthesynclibrarytosafeguardthearrayorslice.Lockthearrayorslicebeforemodifyingit,andunlockitaftermodificationsarema......
  • CF1856B Good Arrays
    题意简述:给定一个序列\(a\),我们定义一个序列\(b\)是好的当且仅当对于\(1\dotsn\)内的每一个\(i\),\(a_i\neqb_i\)且\(\sum_{i=1}^na_i=\sum_{i=1}^nb_i\)(\(a_i\),\(b_i\)均为正整数)。现在有\(T\)组数据,每组数据给定\(n\)和序列\(a\),判断是否存在一个合法的序......