修改密码

【2024周赛课程】内部周赛训练精讲课程

成品课程

陈远龙老师主讲 & 答疑

未购买 · 可先试学5节课

课程目录展开/折叠

2025.01.05-Contest#40-每周六模拟比赛题目讲解

播放快捷键

播放/暂停:空格(或鼠标单击)      全屏:F(或鼠标双击)      退出全屏:Esc

快进10 / 30 / 60秒:方向键→ / Ctrl + 方向键→ / Shift + 方向键→

快退10 / 30 / 60秒:方向键← / Ctrl + 方向键← / Shift + 方向键←

本节课讲解配套PPT&板书:

比赛题目参考源代码

源代码下载:2025.01.05-Contest#40参考源代码下载

1. C1157-找第一个只出现一次的字符-1-TLE
#include <bits/stdc++.h>
using namespace std;

char s[100005];

int main()
{
    cin >> s;

    bool flag = false;
    for (int i = 0; s[i]; i ++)
    {
        int cnt = 0;
        for (int j = 0; s[j]; j ++)
        {
            if (s[j] == s[i]) cnt ++;
        }
        if (cnt == 1)
        {
            cout << s[i];
            flag = true;
            break;
        }
    }

    if (!flag) cout << "no";

    return 0;
}
2. C1157-找第一个只出现一次的字符-2
#include <bits/stdc++.h>
using namespace std;

char s[100005];
int cnt[26];

int main()
{
    cin >> s;
    for (int i = 0; s[i]; i ++)
    {
        cnt[s[i] -'a']++;
    }

    bool flag = false;
    for (int i = 0; s[i]; i ++)
    {
        if (cnt[s[i] - 'a'] == 1)
        {
            cout << s[i];
            flag = true;
            break;
        }
    }

    if (!flag) cout << "no";

    return 0;
}
3. C1158-蛇形填充数组-1
#include <bits/stdc++.h>
using namespace std;

int a[15][15];

int main()
{
    int n;
    cin >> n;
    int cnt = 1;
    a[1][1] = cnt++;
    int x = 1, y = 1;
    while (cnt <= n * n)
    {
        // 向右 1步
        y ++; 
        if (y <= n) a[x][y] = cnt++;
        // 左下
        while (x + 1 <= n && y - 1 >= 1)
        {
            x ++;
            y --;
            a[x][y] = cnt++; 
        }
        // 向下 1步 
        x ++;
        if (x <= n) a[x][y] = cnt++;

        // 右上 
        while (x - 1 >= 1 && y + 1 <= n)
        {
            x --;
            y ++;
            a[x][y] = cnt++;
        }
    }

    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
        {
            cout << a[i][j] << ' ';
        }
        cout << endl;
    }
    return 0;
}
4. C1158-蛇形填充数组-2
#include <bits/stdc++.h>
using namespace std;

int a[15][15];

int main()
{
    int n;
    cin >> n;
    bool flag = true; // flag代表是在最长的对角线的上面还是下面 
    int c = 0; // 每条对角线需要写的个数
    int cnt = 1; // 每个位置上写的数 
    int x, y; // 填表的坐标点 
    for (int i = 1; i <= 2 * n - 1; i ++)
    {
        if (flag) c ++;
        else c --;

        if (flag && (i & 1))
        {
            x = i;
            y = 1;
        }
        if (flag && !(i & 1))
        {
            x = 1;
            y = i;
        }

        if (!flag && (i & 1))
        {
            x = n;
            y = i + 1 - n;
        }

        if (!flag && !(i & 1))
        {
            x = i + 1 - n;
            y = n;
        }  

        for (int j = 1; j <= c; j ++)
        {
            a[x][y] = cnt++;
            if (i & 1) x--, y ++;
            else x++, y--;
        }
        // cout << x << ' ' << y << endl;
        // cout << c << endl;

        if (i == n) flag = false;
    }

    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
        {
            cout << a[i][j] << ' ';
        }
        cout << endl;
    }
    return 0;
}
5. C1159-和为0的4个值-1
#include <bits/stdc++.h>
using namespace std;

int a[4005], b[4005], c[4005], d[4005];

int main()
{
    const char endl = '\n';
    ios::sync_with_stdio(0);
    cin.tie(0);
    unordered_map<int, long long> mp; // 这里可以不用long long
    int n;
    cin >> n;
    for (int i = 0; i < n; i ++)
    {
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    }

    for (int i = 0; i < n; i ++)
    {
        for (int j = 0; j < n; j ++)
        {
            mp[a[i] + b[j]]++;
        }
    }

    long long ans = 0; // 注意是long long ,这里必须是long long
    for (int i = 0; i < n; i ++)
    {
        for (int j = 0; j < n; j ++)
        {
            ans += mp[-c[i] - d[j]];
        }
    }
    cout << ans << endl;

    return 0;
}
6. C1160-加油站-1
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10; // 2 * 1e5
int T;
int p[N], q[N];

int main()
{
    cin >> T;
    for (int c = 1; c <= T; c++)
    {
        int n;
        cin >> n;
        for (int i = 0; i < n; i ++)
            cin >> p[i];

        for (int i = 0; i < n; i ++)
            cin >> q[i];
        memcpy(p + n, p, n * sizeof(int)); // 避免环形处理的复杂性 
        memcpy(q + n, q, n * sizeof(int));

        // 枚举每个起点 
        int x;
        for (x = 0; x < n; )
        {
            int nxt = x;
            int left = p[nxt];
            int cnt = 0;
            while (left >= q[nxt])
            {
                left -= q[nxt];
                nxt ++;
                left += p[nxt];
                cnt ++;
                if (cnt == n) break;
            }   
            if (cnt == n) break;
            else x = nxt + 1;
        }
        if (x >= n)
            cout << "Case " << c << ": Not possible" << endl;
        else
            cout << "Case " << c << ": Possible from station " << x + 1 << endl;

    }

    return 0;
} 
7. C1160-加油站-2
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10; 
int T;
int p[N], q[N];

int main()
{
    const char endl = '\n';
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> T;
    for (int c = 1; c <= T; c++)
    {
        int n;
        cin >> n;
        long long sump = 0, sumq = 0;
        for (int i = 1; i <= n; i ++)
        {
            cin >> p[i];
            sump += p[i];
        }

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

        if (sump < sumq) // 肯定跑不完 
        {
            cout << "Case " << c << ": Not possible" << endl;
            continue;
        }

        // 肯定跑得完的情况 
        int sum = 0;
        for (int x = 1; x <= n; x ++)
        {
            bool flag = true;
            for (int i = x; i <= n;i ++)
            {
                sum += p[i] - q[i];
                if (sum < 0)
                {
                    sum = 0;
                    x = i;
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                cout << "Case " << c << ": Possible from station " << x << endl;
                break;
            }
        }
    }

    return 0;
} 

本节课答疑

建议大家有问题先通过AI答疑(比如:DeepSeek 等),AI时代需要学会使用AI辅助学习

陈远龙老师视频讲解:如何使用DeepSeek进行答疑?

通过AI未能获得满意解答的,可以联系陈远龙老师答疑

目录