Codeforces div1.A2. Burenka and Traditions (hard version)

题意: 给定一个数组,要求通过这样一个操作使得a数组全部置0,选定一个区域,异或一个x值,贡献值为区间长度/2向上取整,要求最小置0总贡献值

思路: eazy是dp写的,hard反而只能贪心,引用eazy的结论:我们只看op长度1,2的贡献值,由于较长的操作都可以由他俩转换,所以只看1,2,由于很明显,只有2的操作可以使初始为n的ans减小,观察怎样才能使它减小呢?我们必须要在操作一个连续区间的情况下额外使一个数的异或值变为0,即异或线性相关的连续字段的最大数量,就是ans的减少数量

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>

//#define int ll
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 200010, M = 2 * N, B = N, MOD = 998244353;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int n, m, k;
int a[N];
int prexor[N];

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll mod) {
    ll res = 1;
    while (b) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

inline void init() {}

void slove()
{
    cin >> n; pre(i, 1, n) si(a[i]);

    int ans = n;
    pre(i, 1, n) prexor[i] = a[i] ^ prexor[i - 1];
    
    set<int> val;
    int last = 0;
    val.insert(0);
    pre(i, 1, n)
    {
        if (val.count(prexor[i])) {
            ans--;
            pre(j, last, i-1)
                val.erase(prexor[j]);
            last = i;
        }
        val.insert(prexor[i]);
    }
    cout << ans << endl;
}

signed main()
{
    int _;
    si(_);
    //_ = 1;
    init();
    while (_--)
    {
        slove();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值