跳到正文

个人记录

!?常卡卡常!?

coding_goat约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年4月23日
更新
2026年11月11日
篇幅
约 6 分钟

版本

  1. v3当前
  2. v2
  3. v1

本文主要讲解奇异搞笑做法,时间复杂度如果我没算错的话好像很极限但是很轻松就过了不知道为什么。

把这个看成一个数字,那么我们需要的是两个数异或之后数位为 11 的位置尽可能多。

我们先考虑统计答案,令 cntxcnt_x 为 xx 中 11 的个数,枚举每一个数 xx,对于这个数的补集 x′x' 枚举子集,计算 cntxcnt_x 和枚举的子集中的当前元素 yy 的 cntycnt_y,答案是他们的和。可是这样做会漏掉与 xx 有交的地方。怎么办呢?我们把维护当前集合 xx 可达/不可达变成包含 xx 且不在当前集合里面的 11 的个数的最小值,将该值记录为 ff,若 xx 不可达则 fx=∞f_x=\infty,那么答案就是 max⁡(cntx+cnty−fy)\max(cnt_x+cnt_y-f_y)。对于 cntcnt 的统计可以在 O(c2c)O(c2^c) 预处理解决,总的时间复杂度应该是 O(c2c+3c)O(c2^c+3^c)。但是似乎离跑满差的挺远不预处理都可以在 1.96s 的时间极限跑过。

代码见下:

C++
#include<bits/stdc++.h>

#define pii pair<int,int>
#define pll pair<long long,long long>
#define ll long long
#define i128 __int128

#define mem(a,b) memset((a),(b),sizeof(a))
#define m0(a) memset((a),0,sizeof(a))
#define m1(a) memset(a,-1,sizeof(a))
#define lb(x) ((x)&-(x))
#define lc(x) ((x)<<1)
#define rc(x) (((x)<<1)|1)
#define pb(G,x) (G).push_back((x))
#define For(a,b,c) for(int a=(b);a<=(c);a++)
#define Rep(a,b,c) for(int a=(b);a>=(c);a--)
#define in1(a) a=read()
#define in2(a,b) a=read(), b=read()
#define in3(a,b,c) a=read(), b=read(), c=read()
#define in4(a,b,c,d) a=read(), b=read(), c=read(), d=read()
#define fst first
#define scd second
#define dbg puts("IAKIOI")

using namespace std;

int read() {
	int x=0,f=1; char c=getchar();
	for(;c<'0'||c>'9';c=getchar()) f=(c=='-'?-1:1);
	for(;c<='9'&&c>='0';c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}
void write(int x) { if(x>=10) write(x/10); putchar('0'+x%10); }

const int mod = 998244353;
int qpo(int a,int b) {int res=1; for(;b;b>>=1,a=(a*a)%mod) if(b&1) res=res*a%mod; return res; }
int inv(int a) {return qpo(a,mod-2); }

const int maxn = 200050;

int n,m;
int f[(1<<18)+114],cnt[(1<<18)+114];
int val[maxn];

int calc(int x) {
	int res=0;
	while(x) res+=(x&1),x>>=1;
	return res;
}

string rev(int x) {
	string s="";
	For(i,1,n) {
		if(x&1) s+="1";
		else s+="0";
		x>>=1;
	}
	return s;
}

void work() {
	cin>>n>>m;
	For(i,1,m) {
		string s; cin>>s;
		s=" "+s;
		int res=0;
		For(j,1,n) res=(res<<1)+(s[j]=='G');
		val[i]=res;
	}

	For(i,0,(1<<n)-1) cnt[i]=calc(i);

	mem(f,0x3f);
	For(i,1,m) {
		int qwq=cnt[val[i]];
		f[0]=min(f[0],qwq);
//		cout<<rev(val[i])<<":\n";
		for(int S=val[i];S>0;S=val[i]&(S-1)) {
//			cout<<"    "<<rev(S)<<'\n';
			f[S]=min(f[S],qwq-cnt[S]);
		}
	}
	For(i,1,m) {
		int St=((1<<n)-1)^val[i];
		int qwq=cnt[St];
		int res=-f[0];
		for(int S=St;S>0;S=St&(S-1)) {
			res=max(res,cnt[S]-f[S]);
		}
		cout<<cnt[val[i]]+res<<'\n';
	}
}

signed main() {
//	freopen("data.in","r",stdin);
//	freopen("myans.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	double stt=clock();
	int _=1;
//	_=read();
//	cin>>_;
	For(i,1,_) {
		work();
	}
	cerr<<"\nTotal Time is:"<<(clock()-stt)*1.0/1000<<" second(s)."<<'\n';
	return 0;
}
原文