题解:P13781 [eJOI 2022] Adjacent Pairs

· · 题解

P13781

::::warning[关于这个做法] 此做法无正确性,只是很难卡。我机房的同学已经卡掉了。

hack 数据生成代码(由 deepseek 生成)

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

int main() {
    freopen("hack.in","w",stdout);
    int n = 200000;
    vector<int> a(n + 1);
    int pos = 1;

    // 阶段 1:前 4900 个位置
    // 对每个 (X,Y) ∈ [51,100]2, X≠Y 放一个长度 2 的交叉段
    // 交叉段格式:奇数位 = Y,偶数位 = X
    for (int X = 51; X <= 100; X++) {
        for (int k = 1; k <= 49; k++) {
            int Y = X + k;
            if (Y > 100) Y -= 50; // 循环,Y 遍历 [51,100] 中除 X 外的所有值
            a[pos++] = Y; // 奇数位
            a[pos++] = X; // 偶数位
        }
    }
    // 现在 pos = 4901

    // 阶段 2:47550 个配对
    // 奇数位 ∈ [1,50],偶数位 ∈ [51,100]
    for (int j = 1; j <= 47550; j++) {
        a[pos++] = ((j - 1) % 50) + 1;   // 1..50
        a[pos++] = ((j - 1) % 50) + 51;  // 51..100
    }

    // 阶段 3:2450 个配对
    // 奇数位 ∈ [1,50],偶数位 ∈ [1,50],偏移 25 避免配对内相等
    for (int j = 47551; j <= 50000; j++) {
        a[pos++] = ((j - 47551) % 50) + 1;          // 1..50
        a[pos++] = ((j - 47551 + 25) % 50) + 1;     // 1..50,偏移 25
    }

    // 阶段 4:47550 个配对
    // 奇数位 ∈ [51,100],偶数位 ∈ [1,50]
    for (int j = 50001; j <= 97550; j++) {
        a[pos++] = ((j - 50001) % 50) + 51;         // 51..100
        a[pos++] = ((j - 47551 + 25) % 50) + 1;     // 1..50,与阶段 3 连续循环
    }

    // ===== 验证 =====
    // 1. 数组“良好”
    for (int i = 1; i < n; i++) {
        if (a[i] == a[i + 1]) {
            fprintf(stderr, "FAIL: adjacent conflict at %d: %d\n", i, a[i]);
            return 1;
        }
    }

    // 2. 每个值在奇数位、偶数位都恰好出现 1000 次
    vector<int> co(101, 0), ce(101, 0);
    for (int i = 1; i <= n; i++) {
        if (i % 2) co[a[i]]++;
        else ce[a[i]]++;
    }
    for (int v = 1; v <= 100; v++) {
        if (co[v] != 1000 || ce[v] != 1000) {
            fprintf(stderr, "FAIL: count mismatch for %d: o=%d e=%d\n", v, co[v], ce[v]);
            return 1;
        }
    }

    fprintf(stderr, "OK: n=%d, pos=%d\n", n, pos);

    // 输出
    printf("1\n%d\n", n);
    for (int i = 1; i <= n; i++) printf("%d ", a[i]);
    printf("\n");
    return 0;
}

::::

题意

定义序列为好的(保证最初的为好的),即整个序列中都没有相邻的相同的值。问当前序列最少需要多少次操作,使得每次操作后序列都是好的,且最终的序列中只有 2 种值。

做法

::::info[提示] 本文思考链路是作者赛时的思考链路。 ::::

首先可以明显发现最终序列肯定是一个由 2 种数交替出现构成的序列,故可以选取序列中奇数位出现最多的数为最终奇数位的值,偶数位出现最多的数为最终偶数位的值。

然后发现可能会因为偶数位出现最多的数和奇数位最多的数是一样的,导致序列不是好的。故可以选出两种位置出现次数前 2 大的数。

再然后会发现这样依旧是不对的,题目中说。

使得每次操作后序列都是好的

而此时,设当前奇数位的最终值是 a,偶数位最终值为 b。序列中出现

\ldots ab\ldots

且 a 在偶数位,b 在奇数位时,这就无法用 2 次操作变成

\ldots ba\ldots

因为无论以什么操作顺序都是不合法的。此时就会需要额外的 1 次操作。而这个就会影响答案,使得答案不一定是在出现次数排前 2 的数中出现。

于是考虑人类智慧。我选择枚举奇偶数位中出现次数的前 50 个配对计算答案。每次枚举配对后,在序列中寻找连续的会需要额外次数的段,形如

\underbrace{\ldots}_{\text{奇数个数}} \underbrace{aba\ldots baba}_{\text{长度为 }k}\ldots

然后额外操作 \left\lfloor\frac{k}{2}\right\rfloor 次即可。 ::::info[关于为什么是 \left\lfloor\frac{k}{2}\right\rfloor 次] 要破坏掉这些互相牵制的位置,只需每 2 个数有 1 个改成别的数即可,而 k 为奇数时,只需改第 2,4,6,8,\ldots 个数即可。 :::: 此时运行次数为 2\times 10^5\times 50^2=5\times 10^8 可以通过。

时空复杂度

时间:2500\cdot n

空间:O(n)

code(有解释,上面看不懂可以看代码)

马蜂有点丑别喷

//
#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e5+5;

map<int,int> mp1,mp2; // 存储每个数出现次数 
vector<pair<int,int> > v1,v2; // 排序每个数出现次数 
int a[MAXN];

int main(){
    int t;
    scanf("%d",&t);
    while (t--){
        mp1.clear();
        mp2.clear();
        v1.clear();
        v2.clear(); //初始化 
        int n;
        scanf("%d",&n);
        for (int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            if (i%2) mp1[a[i]]++;
            else mp2[a[i]]++;
        }
        for (pair<int,int> i:mp1) v1.push_back({i.second,i.first}); // 把次数放在前方便排序 
        for (pair<int,int> i:mp2) v2.push_back({i.second,i.first});
        sort(v1.begin(),v1.end(),greater<pair<int,int> >());
        sort(v2.begin(),v2.end(),greater<pair<int,int> >());
        int ans=n;
        for (int i=0;i<min(50,(int)v1.size());i++)
            for (int j=0;j<min(50,(int)v2.size());j++){ // 枚举奇偶位最终值 
                if (v1[i].second==v2[j].second) continue; // 最终值不能相等 
                int tmp=n-v1[i].first-v2[j].first; // 不管过程的情况下的操作次数
                // 有 奇数位最终值出现次数+偶数位最终值出现次数 个数不需要改变
                // 剩下都要改变,所以是n减去这两个 
                int las=0; // 当前连续的abababa的开头的位置 
                for (int k=1;k<=n;k++){
                    if (k%2){
                        if (a[k]==v2[j].second and las==0) 
                        // 当前位属于ababababa中的一部分,但是前面没有连续的,所以新开一块 
                            las=k;
                        if (a[k]!=v2[j].second and las!=0)
                        // 当前位不属于ababababa的一部分,但是前面有一个连续的块,进行结算 
                            tmp+=(k-las)/2,las=0;
                    }
                    else{
                        if (a[k]==v1[i].second and las==0)
                        // 当前位属于ababababa中的一部分,但是前面没有连续的,所以新开一块 
                            las=k;
                        if (a[k]!=v1[i].second and las!=0)
                        // 当前位不属于ababababa的一部分,但是前面有一个连续的块,进行结算 
                            tmp+=(k-las)/2,las=0;
                    }
                }
                if (las) tmp+=(n-las+1)/2,las=0; // 不要忘记把最后一块计算了!!!
                // PS:赛时没写这句过了大样例,最后只有10pts,加上这句就过了 
                ans=min(ans,tmp);
            }
        printf("%d\n",ans);
    }

    return 0;
}