2026-10-9 比赛总结

· · 个人记录

比赛分数

题号 分数
\textcolor{orange}{\texttt{A}} \textcolor{orange}{40}
\textcolor{orange}{\texttt{B}} \textcolor{green}{100}
\textcolor{orange}{\texttt{C}} \textcolor{green}{100}
\textcolor{yellow}{\texttt{D}} \textcolor{red}{0}

错题总结

A - 中秋灯会

错误代码

#include <iostream>
using namespace std;

const int MOD = 38273946;
const int N = 35;

int color[N][N];
int charge[N][N][3];

const int dx[] = {0,1,0,0};
const int dy[] = {1,0,-1,1};

int n,m;
int count(){
    int ans = 0;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            for(int k = 0;k < 2;k++){
                int xx = i + dx[k];
                int yy = j + dy[k];

                if(xx < 1 || xx > n || yy < 1 || yy > m){
                    continue;
                }

                if(color[xx][yy] == color[i][j]){
                    ans++;
                }
            }
            ans %= MOD;
        }
    }

    return ans;
}

int main(){
    cin >> n >> m;

    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            cin >> color[i][j];
            color[i][j]--;
        }
    }

    int cx = 0,cy = 0,tar = 0;
    int maxn = 0;

    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            for(int k = 0;k < 3;k++){
                for(int x = 0;x < 4;x++){
                    int xx = i + dx[x];
                    int yy = j + dy[x];

                    if(xx < 1 || xx > n || yy < 1 || yy > m){
                        continue;
                    }

                    if(color[xx][yy] == k){
                        charge[i][j][k]++;
                    } 
                }

                if(charge[i][j][k] > maxn){
                    maxn = charge[i][j][k];
                    cx = i,cy = j;
                    tar = k;
                }
            }
        }
    }

    int org = color[cx][cy];
    color[cx][cy] = tar;
    int charge_ans = count();

    color[cx][cy] = org;
    cout << max(count(),charge_ans);

    return 0;
}

错误原因

一大堆问题:

核弹瘫痪,椅子爆炸。

题意概括

给定一个 n \times m 的矩阵,每个格子有颜色 (1,2,3)。

统计“同色灯对”:同一行中左右相邻且颜色相同,或同一列中上下相邻且颜色相同,每对只算一次。

你可以 至多修改一个格子 的颜色。求修改后,矩阵中同色灯对的最大数量。

思路

纯暴力模拟即可,对于每个格子,枚举每个格子变换的颜色,然后重新算一遍“同色灯对”,在所有答案中取最大值。

至于如何避免重复,我们可以只和右边的和下面的灯笼尝试做配对,因为上面的和左边的已经被之前的格子配对过了。

然后题目还说可以不改动,但我们之前修改的时候,把颜色改成本身的颜色就可以视为不改,所以无需额外判断。

复杂度

正确代码

#include <iostream>
using namespace std;

const int N = 35;

int color[N][N];

const int dx[] = {0,1}; // 只跟下面和右边的灯笼尝试配对
const int dy[] = {1,0};

int main(){
    int n,m;
    cin >> n >> m;

    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            cin >> color[i][j];
        }
    }

    int ans = 0;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            int col = color[i][j]; // 记录原本颜色
            for(int k = 1;k <= 3;k++){        
                color[i][j] = k; // 修改颜色
                int cnt = 0;
                for(int x = 1;x <= n;x++){ // 重新计算
                    for(int y = 1;y <= m;y++){
                        for(int dir = 0;dir < 2;dir++){
                            int xx = x + dx[dir];
                            int yy = y + dy[dir];

                            if(xx > n || yy > m) continue; // 边界判断
                            if(color[x][y] == color[xx][yy]){
                                cnt++;
                            }
                        }
                    }
                }
                ans = max(ans,cnt);
            }
            color[i][j] = col; // 还原颜色,避免影响下次操作
        }
    }

    cout << ans;

    return 0;
}

T4 - 中位数

错误原因

没思路,况且做到那个时候也没时间了,跟 T2 苦战 40min。

题意概括

给定一个长度为 n 的整数序列 a。你可以把 a 任意打乱重排,得到新序列 b_1,b_2,\cdots,b_n。

定义 c_i = b_i - i。 把 c 从小到大排序后,取第 \left \lfloor \frac{n + 1}{2} \right \rfloor 个位置的元素作为 c 的“中位数”。

求在所有可能的排列方案中,“中位数”能达到的最大值。

思路

首先,不难想到,答案一定在 [(\min a) + 1,(\max a) - 1] 中。

然后,考虑到中位数 m 想要大于 x 的话,就等价于需要让至少 \lfloor n \div 2 \rfloor + 1 个数大于 x。

那我们如何判断能否找出 \lfloor n \div 2 \rfloor + 1 个数全部大于 x 呢?我们只需要用 双指针,指针 r 表示下一个位置,能放的话就 r++,否则就把 c[l] 扔掉即可。

又观察到,答案存在单调性,所以我们可以用二分答案来找,找完后 l 就是答案。因为是找答案,而答案有范围,所以二分的范围就是 [-10^9,10^9]。

:::info[注意] 最开始 a 要先保证单调递增,所以先要排序。 :::

复杂度

正确代码

#include <iostream>
#include <algorithm>
using namespace std;

#define int long long

const int N = 1e5 + 10;
int a[N];

int n;
bool check(int mid){
    int cnt = 0;
    int l = 1,r = 1;

    while(l <= n){
        if(a[l] - mid >= r){
            l++,r++;
            cnt++;
        } else{
            l++; // 丢掉 a[l]
        }
    }

    return cnt * 2 > n; // 中位数是否能大于或等于 mid
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;

    for(int i = 1;i <= n;i++){
        cin >> a[i];
    }

    sort(a + 1,a + n + 1); // 先排序所有数

    int l = -1e9,r = 1e9;
    while(l < r){ // 二分找答案
        int mid = (l + r + 1) / 2;

        if(check(mid)) l = mid;
        else r = mid - 1;
    }

    cout << l;

    return 0;
}