2026.7.18 总结

· · 个人记录

测试基本情况

虽然但是有点离谱

整体状态

挺好的,虽然说我在读 A 题的时候有 \texttt{1.4} 了,但后面 \texttt{3} 题十分简单,直接得吃。

题目复盘

A: \texttt{drought}

初始思路

本来想着打暴力,结果发现有点难写(?,就骗了分。 。 。

解题过程

读题 \to 试打暴力 \to 骗分

考场代码

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


signed main() {
    freopen("drought.in", "r", stdin);
    freopen("drought.out", "w", stdout);
    int t;
    cout << "14\n16\n-1\n-1\n-1";
    return 0;
}

赛后题解

我们思考一下,这个题在哪里把你卡住了,肯定是你不知道每个元素最终的值。然后我们可以想到枚举最终值,但是数据范围很不友好,足足有 10^9,效率太低。

那怎么做呢?我们试着找找规律。

为了方便后面表示,我们设最终值为 x。

然后我们就会发现一个规律:当位置 i 为奇数时,算式末尾总会有一个 -x,而且当 n 为奇数时,x 的值是可以确定的;当 i 为偶数时,算式与 x 无关,而且当 n 为偶数时,x 的值是可以等于任何数的。而偶数位置和奇数位置的元素和是必须相等的。

而且,因为每次只能将相邻两个位置同时减少,所以必须确保任意相邻两位的操作次数不少于 0。
所以,我们需要对所有带 x 的操作次数统计最小值,而这个就是 x 的最大取值,同时还需要对所有不带 x 的位置进行检查。最终还需要对 x 进行检查,因为饥饿度也不能为负。

补题代码

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

const int kN = 1e5 + 5;
int n;
int a[kN];
void work() {
    cin >> n;
    memset(a, 0x3f, sizeof a);
    int ans = 0;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    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;
        }
    }
    for(int i = 1; i < n; i++)
        if(a[i] != a[i + 1] || a[i] < 0) {
            cout << "-1\n";
            return;
        }
    cout << ans << "\n";
}
signed main() {
    int t;
    cin >> t;
    while(t--)
        work();
    return 0;
}

时间复杂度

O(N)

本题总结

这道题代码简单,思维复杂。

让我明白了有些题是可以在草稿纸上算一算、写一写,找找规律的。

B:\texttt{dice}

初始思路

这题就读题有点困难,但理解之后十分简单。

我想着每个骰子都只有 4 个面,就枚举构造,然后检查是否合法即可。

解题过程

读题 \to 打代码

考场代码

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

const int kN = 5;
int a[kN], b[kN], s[kN];
bool query(int x[], int y[]) {
    int cnt1 = 0, cnt2 = 0;
    for (int i = 1; i <= 4; i++)
        for (int j = 1; j <= 4; j++)
            if (x[i] > y[j])
                cnt1++;
            else if (y[j] > x[i])
                cnt2++;
    return cnt1 > cnt2;
}
void work() {
    for (int i = 1; i <= 4; i++)
        cin >> a[i];
    for (int i = 1; i <= 4; i++)
        cin >> b[i];
    bool flag = 0;
    for (int i = 1; i <= 10; i++)
        for (int j = 1; j <= 10; j++)
            for (int k = 1; k <= 10; k++) {
                if (flag == 1) break;
                for (int o = 1; o <= 10; o++) {
                    if (flag == 1) break;
                    s[1] = i, s[2] = j, s[3] = k, s[4] = o;
                    int cnta = query(a, b) + query(a, s);
                    int cntb = query(b, a) + query(b, s);
                    int cntc = query(s, a) + query(s, b);
                    if (cnta == 1 && cntb == 1 && cntc == 1) {
                        flag = 1;
                    }
                }
            }
    if (flag == 1) cout << "yes\n";
    else cout << "no\n";
}
signed main() {
    freopen("dice.in", "r", stdin);
    freopen("dice.out", "w", stdout);
    int t;
    cin >> t;
    while (t--)
        work();
    return 0;
}

赛后题解

没有一点问题,见↑

补题代码

见上↑

时间复杂度

设 V = \texttt{10}个数字,N = \texttt{10}个面,
则时间复杂度为 O(N^2 V^N)。

本题总结

这道题十分简单,只要你搞清楚题意就行。

C:\texttt{herdle}

初始思路

先分别统计猜测矩阵和答案每个字母的个数,绿色高亮肯定就是判断是不是,黄色的话就是在两个矩阵的公共数量。

解题过程

读题 \to 写代码 \to 查错

考场代码

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

const int kN = 5;
int ans1, ans2;
char a[kN][kN], b[kN][kN];
map<char, int> vis1, vis2;
signed main() {
    freopen("herdle.in", "r", stdin);
    freopen("herdle.out", "w", stdout);
    for (int i = 1; i <= 3; i++)
        for (int j = 1; j <= 3; j++) {
            cin >> a[i][j];
            vis1[a[i][j]]++;
        }
    for (int i = 1; i <= 3; i++)
        for (int j = 1; j <= 3; j++) {
            cin >> b[i][j];
            if (a[i][j] == b[i][j]) {
                ans1++;
                vis1[a[i][j]]--;
                vis2[a[i][j]]--;
            }
            vis2[b[i][j]]++;
        }
    for (char i = 'A'; i <= 'Z'; i++)
        ans2 += min(vis1[i], vis2[i]);
    cout << ans1 << "\n" << ans2;
    return 0;
}

赛后题解

完全没有问题,见上↑

补题代码

见上↑

复杂度

设 M = \texttt{26} 个字母,
则时间复杂度为 O(N + M)。

本题总结

提升了我的调试能力。

D:\texttt{fpot}

初始思路

这个时间差,说白了就是y坐标的差值。
然后我们要找最大值和最小值,用单调队列维护。

那怎么找花盆的宽度?
首先,暴力想都不用想,直接pass掉!
然后我想,是不是花盆越大越满足需求?那就不用说了,二分即可。

解题过程

读题 \to 思考正解 \to 写代码

初始代码

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

const int kN = 1e5 + 5;
int n, d;
struct node {
    int x, y;
} a[kN];
bool cmp(const node &x, const node &y) {
    return x.x < y.x;
}
bool check(int x) {
    deque<int> q, q2;
    for (int i = 1; i <= n; i++) {
        while (q.empty() == 0 && a[i].y > a[q.back()].y)
            q.pop_back();
        q.push_back(i);
        while (a[i].x - a[q.front()].x > x)
            q.pop_front();
        while (q2.empty() == 0 && 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[q.front()].y - a[q2.front()].y >= d)
            return 1;
    }
    return 0;
}
signed 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 + 1 + n, cmp);
    int l = 0, r = 1e6 + 1;
    while (l + 1 < r) {
        int mid = (l + r) / 2;
        if (check(mid) == 1) r = mid;
        else l = mid;
    }
    cout << r;
    return 0;
} 

赛后题解

其实就跟我上面差不多,而我忘了一个东西——判 \texttt{-1}!!!。

我无疑是非常的“高兴”好吧。话说怎么只有一个点是 \texttt{-1} ?

当然,这题也可以用ST表。

补题代码

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

const int kN = 1e5 + 5;
int n, d;
struct node {
    int x, y;
} a[kN];
bool cmp(const node &x, const node &y) {
    return x.x < y.x;
}
bool check(int x) {
    deque<int> q, q2;
    for (int i = 1; i <= n; i++) {
        while (q.empty() == 0 && a[i].y > a[q.back()].y)
            q.pop_back();
        q.push_back(i);
        while (a[i].x - a[q.front()].x > x)
            q.pop_front();
        while (q2.empty() == 0 && 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[q.front()].y - a[q2.front()].y >= d)
            return 1;
    }
    return 0;
}
signed 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 + 1 + n, cmp);
    int l = 0, r = 1e6 + 1;
    while (l + 1 < r) {
        int mid = (l + r) / 2;
        if (check(mid) == 1) r = mid;
        else l = mid;
    }
    if (l == 1e6) cout << -1;
    else cout << r;
    return 0;
} 

复杂度

设 V 为最大花盆长度,
则时间复杂度为 O(N \log_2 V)。

本题总结

让我知道了读题很重要。 。 。

整体反思

做的较好的地方

基本后面 \texttt{3} 道题都做出来了。

需要改进的地方

我检查的不够到位。(摔键盘)

时间安排总结

其实挺好的,虽然有点“偏心”(?