跳到正文

题解

题解:P17247 【Gensokyo OI Round 2】奇迹的诅咒

liuli688约 16 分钟
原文
文章信息与版本

文章信息

发布
2026年7月2日
收录
2026年8月12日
更新
2026年11月13日
篇幅
约 16 分钟
题目
P17247

版本

  1. v2当前
  2. v1

这是官方题解喵!

Sol.1 by @liuli688 & @zhoumurui

考虑操作的性质。(x,y)(x, y) 变为 (xand⁡y,xor⁡y)(x \operatorname{and} y, x\operatorname{or} y) 在二进制位上的表现是如果这位是 (1,0)(1, 0),那么换成 (0,1)(0, 1)。这样,11 只会向右移动。

每一个二进制位是独立的,所以按位考虑。

一个关键观察是,操作到不能操作为止一定是最优的。对于这个二进制位,答案是 11 显然是好的。那么 kk 在哪些位置可以让前缀或等于后缀与等于 11 呢?后缀与是 11 要求后缀全部是 11,那么最小可能的 kk 就是最后一个 00 的位置加上 11。因此可能的 kk 也是一段后缀。只要把所有 11 都放在最右边,那么最后一个 00 的位置就最靠左了,可能的 kk 也最多了嘛!很简单吧。

于是我们可以直接把每一位的所有 11 放到最右侧,算出来最终的数组,然后预处理前缀或与后缀与枚举 kk 就行了……吗?

Code by @zhoumurui

时间复杂度 O(Nlog⁡2N)\mathcal O(N\log^2N),空间复杂度 (3+log⁡A)N(3+\log A)N 个 int。代码中,数组范围并未达到实际的 NN 的范围。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a[200005],b[30][200005];
int c[200005],d[200005];
void solve(){
    cin>>n;
    for (int i=1;i<=n;i++)cin>>a[i];
    for (int i=29;i>=0;i--){
        for (int j=1;j<=n;j++){
            b[i][j]=(a[j]>>i)&1;
        }
        sort(b[i]+1,b[i]+1+n);
    }
    for (int i=1;i<=n;i++){
        a[i]=0;
        for (int j=29;j>=0;j--){
            a[i]+=(b[j][i]<<j);
        }
        //cout<<a[i]<<" ";
    }
    //cout<<"\n";
    c[1]=a[1];
    for (int i=2;i<=n;i++){
        c[i]=c[i-1]|a[i];
    }
    d[n]=a[n];
    for (int i=n-1;i>=1;i--){
        d[i]=d[i+1]&a[i];
    }
    int ans=-1;
    for (int i=1;i<n;i++){
        if (c[i]==d[i+1])ans=max(ans,c[i]);
    }
    cout<<ans<<"\n";
}
signed main(){
    int t;cin>>t;
    while (t--)solve();
    return 0;
}
Code by @liuli688

时间复杂度 Θ(Nlog⁡N)\Theta (N\log N),空间复杂度 2N2N 个 int。

C++
#include <bits/stdc++.h>
using namespace std;

const int N = 5e6 + 10;
int T, n, a[N], b[N], ans;

signed main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	for (cin >> T; T--; ) {
		cin >> n;
		for (int i = 1; i <= n; ++i)
			cin >> a[i];
		for (int j = 0, c; j < 30; ++j) {
			c = 0;
			for (int i = 1; i <= n; ++i)
				c += a[i] >> j & 1;
			for (int i = 1; i <= n - c; ++i)
				a[i] &= ~(1 << j);
			for (int i = n - c + 1; i <= n; ++i)
				a[i] |= 1 << j;
		}
		b[n] = a[n];
		for (int i = n - 1; i; --i)
			b[i] = b[i + 1] & a[i];
		for (int i = 2; i <= n; ++i)
			a[i] |= a[i - 1];
		ans = -1;
		for (int i = 1; i < n; ++i)
			if (a[i] == b[i + 1])
				ans = max(ans, a[i]);
		cout << ans << '\n';
	}
	return 0;
}

这样是不行的,需要两个数组。而空间只允许开下一个数组。

其实,最终的状态由于所有 11 都在最右侧,因此 AA 不仅是单调的还是按位单调的,即有 Aiand⁡Ai+1=Ai,Aior⁡Ai+1=Ai+1A_i \operatorname{and} A_{i+1} = A_i, A_i \operatorname{or} A_{i+1} = A_{i+1}。那么 or⁡i=1kAi=Ak,and⁡i=k+1nAi=Ak+1\operatorname{or}_{i=1}^kA_i=A_k,\operatorname{and}_{i=k+1}^nA_i=A_{k+1}。因此不需要预处理了,只需要一个数组。

Code by @liuli688
C++
#include <bits/stdc++.h>
using namespace std;

const int N = 5e6 + 10;
int T, n, a[N], ans;

signed main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	for (cin >> T; T--; ) {
		cin >> n;
		for (int i = 1; i <= n; ++i)
			cin >> a[i];
		for (int j = 0, c; j < 30; ++j) {
			c = 0;
			for (int i = 1; i <= n; ++i)
				c += a[i] >> j & 1;
			for (int i = 1; i <= n - c; ++i)
				a[i] &= ~(1 << j);
			for (int i = n - c + 1; i <= n; ++i)
				a[i] |= 1 << j;
		}
		ans = -1;
		for (int i = 1; i < n; ++i)
			if (a[i] == a[i + 1])
				ans = max(ans, a[i]);
		cout << ans << '\n';
	}
	return 0;
}

Sol.2 by @zhoumurui

如果你没有观察到性质……

前后缀并不是会用到很多次的。每一个位置只会在计算答案时被用一次。但是我们又不能每次重新计算这个值,这样虽然空间是常数但是时间是平方。那么,怎么平衡时间和空间呢?

考虑每次只算一半。用 CF1400E/P2101 的分治技巧,每次对于完整区间,只计算 kk 为区间中点的答案,然后分别对 kk 在中点两侧的答案取 max⁡\max。当 kk 在区间一侧时,另一侧的 and or 值已经在上一层被算过了,因此计算的复杂度折半。

即,递归计算的函数 f(l,r,p,q)f(l,r,p,q) 记录了区间范围 [l,r][l, r] 和左右的计算结果 p=or⁡i=1l−1ai,q=and⁡i=r+1naip=\operatorname{or}_{i=1}^{l-1}a_i, q=\operatorname{and}_{i=r+1}^na_i。每次计算区间中点 m=⌊l+r2⌋m = \lfloor\dfrac{l+r}2\rfloor,算出 p′=or⁡i=lmai,q′=and⁡i=m+1raip'=\operatorname{or}_{i=l}^{m}a_i, q'=\operatorname{and}_{i=m+1}^ra_i 更新 k=mk=m 的答案 por⁡p′p \operatorname{or} p',如果 por⁡p′=q′and⁡qp \operatorname{or} p' = q' \operatorname{and} q。然后我们便可以将区间分成两个区间 [l,m),(m,r][l,m),(m,r],递归计算 f(l,m−1,p,q′and⁡q)f(l,m-1,p,q' \operatorname{and}q) 和 f(m+1,r,por⁡p′,q)f(m+1,r,p\operatorname{or}p',q)。

这样,不消耗额外空间,时间为 T(n)=Θ(n)+2T(n2)=Θ(nlog⁡n)T(n)=\Theta(n)+2T(\dfrac n 2)=\Theta (n \log n)。

Code by @zhoumurui

时间复杂度 Θ(Nlog⁡N)\Theta (N\log N),空间复杂度 Θ(N)\Theta (N)。

C++
#include<bits/stdc++.h>
using namespace std;
int n,a[5000005];
int cal(int l,int r,int L,int R){
    if (l==r)return -1;
    int mid=(l+r)/2;
    int L1=L,R1=R;
    for (int i=l;i<=mid;i++)L1|=a[i];
    for (int i=mid+1;i<=r;i++)R1&=a[i];
    int ans=-1;
    if (L1==R1)ans=L1;
    ans=max(ans,cal(l,mid,L,R1));
    ans=max(ans,cal(mid+1,r,L1,R));
    return ans;
}
void solve(){
    cin>>n;
    for (int i=1;i<=n;i++)cin>>a[i];
    for (int i=29;i>=0;i--){
        int cnt=0;
        for (int j=1;j<=n;j++){
            if ((a[j]>>i)&1)cnt++;
        }
        for (int j=1;j<=n;j++){
            if (j<=n-cnt)a[j]&=(~(1<<i));
            else a[j]|=(1<<i);
        }
    }
    cout<<cal(1,n,0,-1)<<"\n";
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;cin>>t;
    while (t--)solve();
    return 0;
}

Sol.3 by @liuli688

考虑前缀 or 与后缀 and 相等的条件。设选定了一个分界点 kk,前缀 or 与后缀 and 相等。那么对于一个二进制位(共有 cc 次为 11),若这一位为 11,那么依位运算性质,前缀至少有一个 11,后缀必须全为 11,这要求 c≥n−k+1c \ge n - k + 1;若这一位为 00,那么前缀全为 00,后缀必须至少有一个 00,这要求 c<n−kc < n - k。可见 c≠n−kc \ne n - k。

也就是说,只要我们能操作就操作,最后达到的平衡态是最优的,答案的 kk 一定不能有任何 c=n−kc = n - k,且答案由全部 c≥n−k+1c \ge n - k + 1 的位组成。

反过来,只要选取一个 kk,使得没有任何位的 c=n−kc = n - k,最后达到的状态总有前缀 or 与后缀 and 相等。

因此我们计算每个位出现的个数 cbc_b,然后枚举 kk,如果没有任何 cb=n−kc_b = n - k 就给答案与 or⁡{2b∣cb>n−k}\operatorname{or}\{2^b \mid c_b > n - k\} 取 max⁡\max。

这个过程可以直接暴力。

空间复杂度为 O(log⁡A)\mathcal O(\log A)。

Code by @liuli688

时间 Θ(Nlog⁡A)\Theta(N\log A),空间 Θ(log⁡A)\Theta (\log A)。

C++
#include <bits/stdc++.h>
using namespace std;

const int N = 5e6 + 10;
int T, n, a, cnt[30], ans, sum;

signed main(int argc, const char* argv[]) {
	cin.tie(nullptr)->sync_with_stdio(false);
	for (cin >> T; T--; ) {
		cin >> n;
		memset(cnt, 0, sizeof cnt);
		for (int i = 1; i <= n; ++i) {
			cin >> a;
			for (int j = 29; ~j; --j)
				cnt[j] += a >> j & 1;
		}
		ans = -1;
		for (int k = 1; k < n; ++k) {
			sum = 0;
			for (int i = 0; i < 30; ++i) {
				if (cnt[i] == n - k) {
					sum = -1;
					break;
				}
				sum |= (cnt[i] > n - k) << i;
			}
			ans = max(ans, sum);
		}
		cout << ans << '\n';
	}
	return 0;
}

Sol.4 by @ni_ju_ge

可以直接找合法的 kk。

根据我的理解,必须让前后缀 or and 相等,那么一个一个推进后缀最小长度减一 nw,如果最小长度大于等于后缀 11 个数那就是不能取的(kk 小于最后一个 00 的位置)。

逐步排除不合法的位即可。

时间 O(Nlog⁡A)\mathcal O(N\log A),空间 Θ(N+log⁡A)\Theta(N+\log A)。

Code by @ni_ju_ge
C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ri register int
#define ld long double
#define stg string
#define Cn const
using namespace std;
int read() {
    int r=0,flag=1;char c = getchar();
    while(c<'0'||c>'9') {if(c=='-') flag=-1;c=getchar();}
    while(c<='9'&&c>='0') {r=(r<<1)+(r<<3)+(c-'0');c=getchar();}
    return r*flag;
}
const int N=2e5+5,LOG=31;
int a[N],n,ans;
struct node {
    int dat,id;
    bool operator <(Cn node &x) Cn {
        return dat<x.dat;
    }
} c[LOG+5];
int main() {
    int T;
    cin>>T;
    while(T--) {
        cin>>n;
        for(int i=1;i<=n;i++) a[i]=read();
        for(int i=0;i<LOG;i++) {
            int j=(1<<i);
            c[i+1].id=j,c[i+1].dat=0;
            for(int t=1;t<=n;t++) if(a[t]&j) c[i+1].dat++;
        }
        sort(c+1,c+LOG+1);
        int pos=1,nw=0,ans=(1<<LOG)-1;
        while(c[pos].dat<=nw&&nw<n&&pos<=LOG) {
            ans-=c[pos].id;
            pos++;
            if(pos>LOG) break;
            //cout<<c[pos].id<<" "<<c[pos].dat<<" "<<nw<<endl;
            if(c[pos].dat!=c[pos-1].dat) nw++;
            if(nw>=n-1) {
                ans=-1;
                break;
            }
        }
        cout<<ans<<endl;
    }
}
原文