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

· · 题解

这是官方题解喵!

Sol.1 by @liuli688 & @zhoumurui

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

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

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

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

::::info[Code by @zhoumurui] 时间复杂度 \mathcal O(N\log^2N),空间复杂度 (3+\log A)N 个 int。代码中,数组范围并未达到实际的 N 的范围。

#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;
}

:::: ::::info[Code by @liuli688] 时间复杂度 \Theta (N\log N),空间复杂度 2N 个 int。

#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;
}

::::

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

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

::::success[Code by @liuli688]

#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 的分治技巧,每次对于完整区间,只计算 k 为区间中点的答案,然后分别对 k 在中点两侧的答案取 \max。当 k 在区间一侧时,另一侧的 and or 值已经在上一层被算过了,因此计算的复杂度折半。

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

这样,不消耗额外空间,时间为 T(n)=\Theta(n)+2T(\dfrac n 2)=\Theta (n \log n)。 ::::success[Code by @zhoumurui] 时间复杂度 \Theta (N\log N),空间复杂度 \Theta (N)。


#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 相等的条件。设选定了一个分界点 k,前缀 or 与后缀 and 相等。那么对于一个二进制位(共有 c 次为 1),若这一位为 1,那么依位运算性质,前缀至少有一个 1,后缀必须全为 1,这要求 c \ge n - k + 1;若这一位为 0,那么前缀全为 0,后缀必须至少有一个 0,这要求 c < n - k。可见 c \ne n - k。

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

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

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

这个过程可以直接暴力。

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

::::success[Code by @liuli688] 时间 \Theta(N\log A),空间 \Theta (\log A)。

#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

可以直接找合法的 k。

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

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

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

::::success[Code by @ni_ju_ge]

#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;
    }
}

::::