首页 > 其他分享 >cf1446C. Xor Tree

cf1446C. Xor Tree

时间:2023-11-07 18:34:15浏览次数:36  
标签:Xor s0 Tree fo ans return cf1446C include define

https://codeforces.com/problemset/problem/1446/C

断断续续想了挺久的,还发现看错题了。

首先一个显然的结论是不会成环,证明显然。

突破口在于从高位往低位考虑,我们按照最高一位的值分成两类,一类是这一位为0,另一类为1,那么显然在我们不进行任何操作的时候,如果两个集合都不为空,那么此时一定不合法,而且我们一定要将其中一个集合的大小调整为1,并且剩下的那个数一定会连到另一个集合的某些数, 并且另一个集合中不存在数会向它连边。

那么直接递归求解即可。
时间复杂度最坏情况 应该是每次对半分,O(nlogn)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<ctime>
#include<unordered_map>
#include<queue>
#include<bitset>
#define A puts("YES")
#define B puts("NO")
#define fo(i,a,b) for (ll (i)=(a);(i)<=(b);(i)++)
#define fd(i,b,a) for (int (i)=(b);(i)>=(a);(i)--)
#define mk(x,y) make_pair((x),(y))
#define eb(x) .emplace_back(x)
#define lc (o<<1)
#define rc (o<<1|1)
using namespace std;
//typedef __int128 i128;
typedef double db;
typedef long long ll;
//const ll mo=1e9+7;
//const int inf=1<<30;
const int N=2e5+5;
int a[N],n,b[50],x;
int nex[N*2],to[N*2],head[N],tot;
int ch[N][35],cnt,c,now,sz[N];
void cmax(int &x,int y){
	x=max(x,y);
}
int solve(int l,int r,int k){
	if (l>r) return 0;
	if (l==r) return 1;
	
	int s0=0,s1=0,ans=0;
	fo(i,l,r) {
		if (!(a[i]&b[k])) s0++;
		else s1++;
	}
	cmax(ans, solve(l,l+s0-1,k-1)+min(1,s1));
	cmax(ans, solve(l+s0,r,k-1)+min(1,s0));
	
	return ans;
}
int main()
{
//	freopen("data.in","r",stdin);
//	freopen("data.out","w",stdout);

	b[0]=1;
	fo(i,1,30) b[i]=b[i-1]*2;
	
	scanf("%d",&n);
	fo(i,1,n) scanf("%d",&a[i]);
	
	sort(a+1,a+n+1);
	
	printf("%d",n-solve(1,n,30)); 
	


	return 0;
}

 
 

标签:Xor,s0,Tree,fo,ans,return,cf1446C,include,define
From: https://www.cnblogs.com/ganking/p/17815622.html

相关文章

  • WPF仿VS TreeView
    [TemplatePart(Name="PART_Content",Type=typeof(ToggleButton))][TemplatePart(Name="Expander",Type=typeof(Panel))]publicclassOTreeViewItem:TreeViewItem{Panel?partContent;ToggleButton?pa......
  • cf1856E2. PermuTree (hard version)(bitset+二进制优化背包+开不同大小bitset)
    https://codeforces.com/contest/1856/problem/E2结论是显然的,关键是有一些科技在里面bitset+二进制优化具体分析可以参考https://codeforces.com/blog/entry/98663简而言之就是可以通过\(O(\frac{C\sqrtC}{w})\)的复杂度判断是否能够获得某种体积开不同大小bitsettemplate......
  • Oracle中B-tree索引的访问方法(十一)-- 索引的分裂行为
    索引的分裂行为当某个索引块中要插入新的索引条目,但其中又没有可用空间时,就会发生索引的分裂。根据分裂发生所在的索引块类型的不同,可以分为在根块上发生的分裂,在分支块上发生的分裂和在叶子块上发生的分裂。下面,就这三种情况做分别介绍。从前面的实验中,我们已经看到,大约每个索引块......
  • A Tour Through TREE_RCU's Expedited Grace Periods (翻译)
    原文:https://www.kernel.org/doc/html/latest/RCU/Design/Expedited-Grace-Periods/Expedited-Grace-Periods.htmlATourThroughTREE_RCU'sExpeditedGracePeriods通过TREE_RCU的加速宽限期进行一次旅行Introduction引言ThisdocumentdescribesRCU'sexpeditedgracep......
  • A Tour Through TREE_RCU's Grace-Period Memory Ordering (翻译)
    原文:https://docs.kernel.org/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.htmlAugust8,2017ThisarticlewascontributedbyPaulE.McKenneyIntroductionThisdocumentgivesaroughvisualoverviewofhowTreeRCU'sgrace-periodmemoryorderi......
  • cf1582F2. Korney Korneevich and XOR (hard version)(暴力优化)
    cf1582F2对于每种数可以维护一个列表v[x],表示到当前位置,最后一个数小于等于x,能够取到的值,对于当前的数ai,我们可以用v[ai]中的值x与ai异或,来更新v[ai+1],v[ai+2]后面的值。然后就是有两个优化,每次我们更新完后,都对v[a[i]]清空,因为只有两个相同数之间的数才对后面可能有贡献,前面的......
  • G. wxhtzdy ORO Tree
    G.wxhtzdyOROTreeAfter(finally)qualifyingfortheIOI2023,wxhtzdywasveryhappy,sohedecidedtodowhatmostcompetitiveprogrammersdo:tryingtoguesstheproblemsthatwillbeonIOI.Duringthisprocess,heaccidentallymadeaproblem,whic......
  • 【刷题笔记】101. Symmetric Tree
    题目Givenabinarytree,checkwhetheritisamirrorofitself(ie,symmetricarounditscenter).Forexample,thisbinarytree[1,2,2,3,4,4,3]issymmetric:1/\22/\/\3443Butthefollowing[1,2,2,null,3,null,3]isnot:1......
  • cf1709E. XOR Tree(启发式合并入门)
    cf1709E.XORTree贪心是显然的,关键是如何合并两棵子树的信息,可以采用启发式合并。#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<map>#include<vector>#include<set>#include<ctime>#include<unordered_ma......
  • Binary Tree Level Order Traversal
    SourceGivenabinarytree,returnthelevelordertraversalofitsnodes'values.(ie,fromlefttoright,levelbylevel).ExampleGivenbinarytree{3,9,20,#,#,15,7},3/\920/\157returnitslevelordertraversala......