Codeforces Round #814 (Div. 2)

比赛链接

Codeforces Round #814 (Div. 2)

D2. Burenka and Traditions (hard version)

给出 \(n\) 个数,每次可以选择一个区间和一个数,将该区间的所有数异或上该数,代价为区间数除 \(2\) 的上取整,求将所有区间变为 \(0\) 的最少代价

解题思路

贪心

可以发现,上取整的结果都可以分为长度为 \(2\)\(1\) 的组合,而对于一段区间,例如 \(a,b,c\),如果有 \(a\bigoplus b=c\),则其贡献可以减一,故可以发现一个贪心策略:维护前缀异或和,找不相交的两个相同的数,用 \(set\) 维护每段,\(for\) 一遍即可

  • 时间复杂度:\(O(nlogn)\)

代码

// Problem: D2. Burenka and Traditions (hard version)
// Contest: Codeforces - Codeforces Round #814 (Div. 2)
// URL: https://codeforces.com/contest/1719/problem/D2
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

int t,n,x;
int main()
{
    for(cin>>t;t;t--)
    {
    	cin>>n;
    	set<int> s;
    	s.insert(0);
    	int sum=0,res=n;
    	for(int i=1;i<=n;i++)
    	{
    		cin>>x;
    		sum^=x;
    		if(s.count(sum))res--,s.clear();
    		s.insert(sum);
    	}
    	cout<<res<<'\n';
    }
    return 0;
}
posted @ 2022-08-17 10:12  zyy2001  阅读(328)  评论(0编辑  收藏  举报