2026-7-18 雅礼比赛总结

· · 个人记录

一、比赛基本情况

二、比赛整体回顾

总体还可以,每道题都拿了分,以后可以在阅读理解这方面提升一下。题目总体比以前简单一点,T2、T3 较简单,T1 算是数学题,T4 是难题。时间安排也比以前合理,先做简单的题目,难的题目后面再逐个突破。

三、题目复盘(按题目难度排序)

T1:\texttt{Herdle}

初始思路

按题意模拟,先去去除 \color{green} \footnotesize \text{高亮绿色} 的牛,再用桶记录牛在答案矩阵和猜测矩阵中出现的次数,最后枚举所有牛的种类,\color{red}{\footnotesize \text{累加} \normalsize \space \text{cnt2}_i},答案为和。

考场解题过程

同上。

考场代码

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

#define CLOSE_FREOPEN

const int N = 4;
char ans[N][N],guess[N][N];
int cnt1[30],cnt2[30];

int main(){ 
  #ifndef CLOSE_FREOPEN
    freopen("herdle.in","r",stdin);
    freopen("herdle.out","w",stdout);
  #endif

    int n = 3;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= n;j++){
            cin >> ans[i][j];
            cnt1[ans[i][j] - 'A']++;
        }
    }

    int hl_green = 0,hl_yellow = 0;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= n;j++){
            cin >> guess[i][j];

            if(guess[i][j] == ans[i][j]){
                hl_green++;
                cnt1[guess[i][j] - 'A']--;
            } else{
                cnt2[guess[i][j] - 'A']++;
            }
        }
    }

    for(int i = 0;i < 26;i++){
        if(cnt1[i] <= cnt2[i]){
            hl_yellow += cnt1[i];
        }
   }

    cout << hl_green << '\n' << hl_yellow;

    return 0;
}

赛后题解

同赛时题解,不过应该是累加 \min(\text{cnt1}_i,\text{cnt2}_i)。

补题代码

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

#define CLOSE_FREOPEN

const int N = 4;
char ans[N][N],guess[N][N];
int cnt1[30],cnt2[30];

int main(){ 
  #ifndef CLOSE_FREOPEN
    freopen("herdle.in","r",stdin);
    freopen("herdle.out","w",stdout);
  #endif

    int n = 3;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= n;j++){
            cin >> ans[i][j];
            cnt1[ans[i][j] - 'A']++;
        }
    }

    int hl_green = 0,hl_yellow = 0;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= n;j++){
            cin >> guess[i][j];

            if(guess[i][j] == ans[i][j]){
                hl_green++;
                cnt1[guess[i][j] - 'A']--;
            } else{
                cnt2[guess[i][j] - 'A']++;
            }
        }
    }

    for(int i = 0;i < 26;i++){
        hl_yellow += min(cnt1[i],cnt2[i]);
    }

    cout << hl_green << '\n' << hl_yellow;

    return 0;
}

复杂度

本题总结

T2:\texttt{Non-Transitive Dice}

初始思路

先去做别的题了,没来得及做。但最后输出样例得了 - \hspace{-10px} 10 分。

赛后题解

首先,我们需要搞懂 A 和 B 的关系,应该是看 A 和 B 哪个骰子数字比另外一个骰子数字大的成立个数,谁大谁就可以击败另外那个骰子。当然,特别的是,如果 A 的数比 B 的数大 2 个,B 的数也比 A 的数大两个,那么 A 和 B 为平手。

然后,仅依靠 A 和 B 构造出 C 是很困难的,但是,考虑到数据范围不大,每个面数字最大只有 10,所以,我们可以暴力枚举 C 的四个面,并校验是否为“非传递性”的。

补题代码

(因为时间原因,还没有写)

复杂度

本题总结

T3:\texttt{Drought}

初始思路

如果我们知道了最终的所有数的值,我们就可以快速的得出校验并得出答案,所以,我们可以倒序枚举 i,如果可以将数组可以减少到 i,答案就是

那数组上限和下限是多少呢?显而易见,上限是最小的 $h_i$,下限是 $0$。 #### 考场解题过程 同上。 #### 考场代码 ```cpp #include <iostream> #include <cstdio> #include <cmath> using namespace std; #define int long long #define CLOSE_FREOPEN //#define DEBUG const int N = 1e5 + 10; int h[N],tmp[N]; int n; bool check(int mid){ for(int i = 1;i <= n;i++){ tmp[i] = h[i]; } int res = 0; for(int i = 2;i <= n;i++){ int sub_val = tmp[i - 1] - mid; if(tmp[i] - sub_val < mid) return false; tmp[i - 1] = mid; tmp[i] -= sub_val; res += sub_val; } return (tmp[n] == mid); } void solve(){ cin >> n; int minn = 1e9; for(int i = 1;i <= n;i++){ cin >> h[i]; minn = min(minn,h[i]); } for(int i = minn;i >= 0;i--){ if(check(i)){ int ans = 0; for(int j = 1;j <= n;j++){ ans += h[j] - i; } cout << ans << '\n'; return; } } cout << -1 << '\n'; return; } signed main(){ #ifndef CLOSE_FREOPEN freopen("drought.in","r",stdin); freopen("drought.out","w",stdout); #endif #ifdef DEBUG ios::sync_with_stdio(false); cin.tie(0); #endif int t; cin >> t; while(t--){ solve(); } #ifdef DEBUG cout << flush; #endif return 0; } ``` #### 赛后题解 如果我们知道了最终的所有数的值,我们就可以快速的得出校验并得出答案,但因为一个一个枚举太耗时了。所以,我们无妨可以先设 $x$ 为最终值。 - $[1,2]$ 的操作次数为 $a_1 - x$,如果 $n = 1$,那么必须有 $a_1 - x = 0$。 - $[2,3]$ 的操作次数为 $a_2 - a_1$,如果 $n = 2$,那么必须有 $a_2 - a_1 = 0$。 - $[3,4]$ 的操作次数为 $a_3 - (a_2 - a_1) - x$,如果 $n = 3$,那么必须有 $a_3 - (a_2 - a_1) - x = 0$。 - $[4,5]$ 的操作次数为 $a_4 - a_3 - (a_2 - a_1)$,如果 $n = 4$,那么必须有 $a_4 - a_3 - (a_2 - a_1) = 0$。 综上可知,在奇数位时,操作次数里必定有一个 $-x$;偶数位里,$x$ 就和操作次数无关。所以,偶数位和奇数位的元素之和一定是相等的。 此外,因为每次只能将相邻两个位置同时减少,所以必须确保任意相邻两位的操作次数不少于 $0$ 。因此,我们需要对所有带 $x$ 的操作次数统计最小值,同时还需要对所有不带 $x$ 的位置进行校验。因为饥饿度也不能为负,所以我们最终还要对 $x$ 进行校验。 #### 复杂度 - 时间复杂度:$O(n)

补题代码

#include <iostream>
using namespace std;

#define int long long

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

int main(){
//    freopen("drought.in","r",stdin);
//    freopen("drought.out","w",stdout);

    int t;
    cin >> t;
    while(t--){
        int n,ans = 0;
        cin >> n;

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

        if(n == 2){
            cout << (a[1] == a[2] ? 0 : -1) << '\n';
            continue; 
        }

        for(int i = 2;i < n;i++){
            if(a[i] > a[i + 1]){
                int t = a[i] - a[i + 1];
                a[i - 1] -= t;
                a[i] -= t;
                ans += t * 2;
            }

            if(a[i] > a[i - 1]){
                int t = a[i] - a[i - 1];
                a[i + 1] -= t;
                a[i] -= t;
                ans += t * 2;
            }
        }

        for(int i = n - 1;i > 1;i--){
            if(a[i] > a[i + 1]){
                int t = a[i] - a[i + 1];
                a[i - 1] -= t;
                a[i] -= t;
                ans += t * 2;
            }

            if(a[i] > a[i - 1]){
                int t = a[i] - a[i - 1];
                a[i + 1] -= t;
                a[i] -= t;
                ans += t * 2;
            }
        }

        bool flag = true;
        for(int i = 1;i < n;i++){
            if(a[i] != a[i + 1] || a[i] < 0){
                cout << "-1\n";
                flag = false;
                break;
            }
        }

        if(flag){
            cout << ans << '\n';
        }
    }

    return 0;
}

本题总结

T4:\texttt{Flowerpot S}

初始思路

因为花盆的宽度是单调不减的,所以可以使用二分答案。那二分答案的上界是多少呢?容易想到,花盆的初始坐标不可能摆到 x 坐标大于 \displaystyle \max_{i = 1}^{n} x_i 的地方,因为那里不可能有水,也不可能有解。然后下界就是 0。

然后,在二分里每次检查一个 mid,暴力枚举花盆的初始坐标和可以被花盆接到的水,算出最高和最低的 y。 那为什么不是时间呢?因为每过一个时间单位,水的 y 坐标就会减少 1,所以我们可以把 y 坐标看成时间。这样,就能减少很多计算。

最后,答案就是 r。至于无解,因为 r = 0,所以当检查一直无效时,r 最终会到 0 - 1 的地方,也就是 -1。所以这里我们无需重复判断无解。

考场解题过程

同上。

考场代码

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

#define CLOSE_FREOPEN

struct Coord{
    int x;
    int y;
};

const int N = 1e5 + 10;
Coord pos[N];

int n,d,maxn = 0;
bool check(int len){
    for(int x = 0;x + len <= maxn;x++){ 
        int mintime = 1e9,maxtime = 0;        
        for(int i = 1;i <= n;i++){
            if(pos[i].x >= x && pos[i].x <= x + len){
                mintime = min(mintime,pos[i].y);
                maxtime = max(maxtime,pos[i].y);                
            } 
        }

        if(maxtime - mintime >= d){
            return true;
        }
    }

    return false;
}

int main(){
  #ifndef CLOSE_FREOPEN
    freopen("fpot.in","r",stdin);
    freopen("fpot.out","w",stdout);
  #endif

    cin >> n >> d;

    for(int i = 1;i <= n;i++){
        cin >> pos[i].x >> pos[i].y;
        maxn = max(maxn,pos[i].x);
    }

    int l = 0,r = maxn;
    while(l < r){
        int mid = (l + r) >> 1;

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

    cout << r << ' ';

    return 0;
}

赛后题解

大体框架和上面相同,只是在检查的时候用队列优化了,使得时间复杂度从原本的 \displaystyle O(n \times \max_{i = 1}^{n}) 降至了 O(n)。

补题代码

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

struct Node{
    int x,y;
}; 

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

bool cmp(Node x,Node y){
    return x.x < y.x;
}

int n,d;
bool check(int x){
    deque<int> q1,q2;
    for(int i = 1;i <= n;i++){
        while(!q1.empty() && a[i].y > a[q1.back()].y){
            q1.pop_back();
        }
        q1.push_back(i);
        while(a[i].x - a[q1.front()].x > x){
            q1.pop_front();
        }

        while(!q2.empty() && a[i].y < a[q2.back()].y){
            q2.pop_back();
        }
        q2.push_back(i);
        while(a[i].x - a[q2.front()].x > x){
            q2.pop_front();
        }

        if(a[q1.front()].y - a[q2.front()].y >= d){
            return true;
        }
    }
    return false;
}
int main() {
    // freopen("fpot.in","r",stdin);
    // freopen("fpot.out","w",stdout); 
    cin >> n >> d;

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

    sort(a + 1,a + n + 1,cmp);

    int l = -1,r = 1e6 + 1;
    while(l + 1 < r){
        int mid = (l + r) >> 1;

        if(check(mid)){
            r = mid;
        } else{
            l = mid;
        }
    }

    cout << (r > 1e6 ? -1 : r);

    return 0 ;
}

复杂度

本题总结

四、整体反思

做得较好的地方

每道题都拿了分,没有浪费时间。

需要改进的地方

思维能力还需要加强一下。

时间安排总结

见上方比赛总体总结。