Codeforces 1054D Changing Array

Codeforces 1054D Changing Array



做法:给定一个序列,每个数可以把在2进制k位下取反,也可以不变,在改变后,这个序列异或和不为0的区间的个数。考虑如何求出尽可能少的异或为0的序列,对序列求前缀之后,就相当与问这个前缀的序列中,有多少对的值相同,注意还有开始的0。那么对于所有数取值为min(a,~a),现在我们需要最小化,更新后同一种数中出现的相同的数对的个数,即\(C(a,2) + C(w-a,2)\),w是这种数的个数,a是取反的数的个数,当\(a = \frac{w}{2}\)时,答案最小,对于每一种都计算即可。

#include <bits/stdc++.h>
typedef long long ll;
const int N = 2e5 + 7;
using namespace std;
int n, k, lim, a[N];
map<int,int> M;
ll C(ll x) { return x*(x-1LL)>>1LL; }
int main() {
    scanf("%d%d",&n,&k);
    lim = (1ll<<k) - 1ll;
    for(int i = 1; i <= n; ++i) scanf("%d",&a[i]);
    for(int i = 2; i <= n; ++i) a[i] ^= a[i-1];
    for(int i = 0; i <= n; ++i) a[i] = min(a[i], lim-a[i]), ++M[a[i]];
    ll ans = C(n+1);
    for(map<int,int>::iterator it = M.begin(); it != M.end(); ++it) {
        int w = (*it).second;
        ans -= C( w>>1 ) + C( w - (w>>1) );
    }
    printf("%lld\n", ans);
    return 0;
}

原文地址:https://www.cnblogs.com/RRRR-wys/p/9902824.html

时间: 2024-08-29 16:33:41

Codeforces 1054D Changing Array的相关文章

codeforces 482B. Interesting Array【线段树区间更新】

题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val.就是区间l---r上的与的值为val,最后问你原来的数组是多少?如果不存在输出no 分析:分析发现要满足所有的区间,而一个点上假如有多个区间的话,这个点的值就是所有区间或的值,因为只有这样才能满足所有区间的,把所有位上的1都保存下来了,那么可以发现用线段树来维护,但是那么怎么判断满不满足条件呢?可以也用线段树,更新了之后在整个维护一遍看看满不满足题意,如

Codeforces 482B Interesting Array(线段树)

题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,现在有M个限制,每个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 否有满足的数列. 解题思路:线段树维护,每条限制等于是对l~r之间的数或上q(取且的性质,相应二进制位一定为1),那么处理完所有的 限制,在进行查询,查询对应每个l~r之间的数取且是否还等于q.所以用线段树维护取且和,修改为或操作. #include <cstdio> #include <c

Codeforces 56D Changing a String 编辑距离 dp

题目链接:点击打开链接 编辑距离,,== 一边dp一边记录前驱太累,,还是dp后找路径大法好 #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll int #define N 1010 char s[N], t[N]; int dp[N][N], n, m; // 0为插入 1为删除 2 3为替换 stru

CodeForces E. Lucky Array 幸运数列

CodeForces    E. Lucky Array  幸运数列 Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are n

codeforces 797 E. Array Queries【dp,暴力】

题目链接:codeforces 797 E. Array Queries   题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为止,求每次询问要转换的次数. 题解:纯暴力会TLE,所以在k为根号100000范围内dp打表 dp[i][j]表示初始p为i, k为j,需要转换几次可以大于n. 状态转移方程:dp[i][j] = dp[i+a[i]+j] + 1 #include <cstdio> #include <al

codeforces 407C Curious Array

codeforces 407C Curious Array 参考题解:https://www.cnblogs.com/ChopsticksAN/p/4908377.html 1.杨辉三角可以由多维前缀和求得. 2."搭顺风车"的思想. 3.// 右端点减去的那个数可以用组合数学的方法思考. #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define mp ma

【CodeForces 624D】Array GCD

题 You are given array ai of length n. You may consecutively apply two operations to this array: remove some subsegment (continuous subsequence) of length m < n and pay for it m·a coins; change some elements of the array by at most 1, and pay b coins

Codeforces 86D Powerful array(莫队)

题目链接:http://codeforces.com/problemset/problem/86/D 题目: An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occu

Codeforces 722C Destroying Array(并查集)*

题目链接:http://codeforces.com/problemset/problem/722/C 题意: 有 n 个正整数序列.同时 n 个摧毁序列,从 1 到 n每次把正整数序列里对应下标的数字去掉,成为一个间隔. 问每去掉一个数字,序列中最大的连续子段和. 思路: 倒着想,这道题就变成了:从 n 到 1每次出现一个数字,若数字的两边已经存在集合,则把存在的集合合并为一个, 并更新集合的和,否则单独为一个集合.过程中始终维护序列中目前所有集合里和的最大值. 代码: #include <i