Codeforces Round #613 (Div. 2) D - Dr. Evil Underscores(思维,位运算)

?? ?? ??

题意:对于一个数组,求一个数字与数组每个元素异或之后的最大值最小,求这个最大值

又是位运算,,题目给出数组元素范围在2^30以内,二进制最多30位,从最高位开始贪心,如果此位置的数组元素有的是1有的是0,最后肯定取1,否则取0,还有就是分组讨论,因为每个bit位只能满足原数组中一部分元素异或后为1

#define int ll
vector<int>a;
int solve(vector<int>v,int bit)
{
    if(bit<=0||v.size()==0) return 0;
    vector<int>zero,one;
    rep(i,v.size())
    {
        if((v[i]>>bit)&1) zero.push_back(v[i]);
        else one.push_back(v[i]);
    }
    if(zero.size()==0) return solve(one,bit-1);
    if(one.size()==0) return solve(zero,bit-1);
    return (min(solve(one,bit-1),solve(zero,bit-1))|1<<bit);
}
signed main()
{
    int n;cin>>n;
    a.resize(n);cin>>a;
    cout<<solve(a,30)<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/Herlo/p/12179762.html

时间: 2024-08-27 11:16:10

Codeforces Round #613 (Div. 2) D - Dr. Evil Underscores(思维,位运算)的相关文章

Codeforces Round #613 (Div. 2) D. Dr. Evil Underscores

题目:http://codeforces.com/contest/1285/problem/D 思路:从高位往低位建 \(01\;trie\) 树,从高位 dfs 当只有一个分支,当前位为 \(0\),填法唯一: 当有两个分支,当前位为 \(1\),填法不唯一,则返回较小值: #include<bits/stdc++.h> using namespace std; const int N=1e5+5; int n; int trie[N*31][2]; int cnt; void insert

Codeforces Round #320 (Div. 2) &quot;Or&quot; Game(好题,贪心/位运算/前缀后缀或)

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 typedef long long ll; 7 /* 8 n个数,你最多有k次操作,每次操作可以选择一个数乘以x,问所有数或(|)的最大值 9 贪心思路:选一个数进行k此乘以x操作; 因为x>=2 10 111 ---> 1111 11

Codeforces Round #613 (Div. 2)

https://codeforces.com/contest/1285 A - Mezo Playing Zoma 题意:按一系列的"LR"键,其中一些键可能被忽略执行,求最后分布的位置数量. 题解:肯定是最左和最右夹着的区间,所以统计最左的和最右的位置.最后会发现恰好就是n+1. B - Just Eat It! 题意:问是否有一段subsegment的和>=全体的和,这个subsegment不能取全体. 题解:这题里的subsegment要去掉非空前缀,或者去掉非空后缀,或者

Codeforces Round #613 (Div. 2)D(贪心,分治)

构造两颗深度为30的字典树(根节点分别是0和1),结点只有0和1,从根节点向下DFS,贪心取答案. 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 vector<int>a; 5 int dfs(vector<int>b,int x){ 6 if(x<0||b.size()==0)//30位都枚举完毕或当前向量中没有数字就中止 7 return 0;

Codeforces Round #613 (Div. 2) B. Just Eat It!

Link 题意: 求最大区间和所在区间是不是 \(1 \sim n\) 思路: 设 \(dp[i]\) 是以 \(i\) 为右端的最大区间和 \(dp[i]=max(dp[i-1]+a[i],a[i])\) 代码: #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+5;; int n; ll a[N]; int main() { ios::sync_with_stdio(fals

Codeforces Round #221 (Div. 2) D. Maximum Submatrix 2 (思维题)

题目地址:codeforces 221 D 这场是人生中做的第一场CF中的D题.(当时只做出来了A题..)过年之际回顾了一下,就顺便看了几道D题.现在做CF的D题在比赛时还是做不出来.但是赛后往往都可以自己做出来.据说D题能在比赛中稳出的话就可以区域赛银了.于是争取以后CF能稳出4道题吧. 这道题刚开始不该看标签的..给的是DP..于是就一直朝着DP方向想.但是感觉不像是DP.就换了个思路,就做出来了. 大体方法是先预处理出每一行中每个数向左延伸最长的连续1的个数.然后对每一行的进行排序(我这里

Codeforces Round #548 (Div. 2) F splay(新坑) + 思维

https://codeforces.com/contest/1139/problem/F 题意 有m个人,n道菜,每道菜有\(p_i\),\(s_i\),\(b_i\),每个人有\(inc_j\),\(pref_j\),一个人可以买一道菜的条件是 1. \(p_i \leq inc_j \leq s_i\) 2. \(|b_i - pref_j| \leq inc_j-p_i\) ,问每个人分别能买多少道菜 题解 转化一下公式 \(p_i \leq inc_j \leq s_i\) 下面两个满

Codeforces Round #588 (Div. 2) C. Anadi and Domino(思维)

链接: https://codeforces.com/contest/1230/problem/C 题意: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1≤a≤b≤6, there is exactly one domino with a dots on one half and b dots on th

Codeforces Round #242 (Div. 2)C(找规律,异或运算)

一看就是找规律的题.只要熟悉异或的性质,可以秒杀. 为了防止忘记异或的规则,可以把异或理解为半加运算:其运算法则相当于不带进位的二进制加法. 一些性质如下: 交换律: 结合律: 恒等律: 归零律: 典型应用:交换a和b的值:a=a^b^(b=a); #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<