跳到正文

个人记录

CSP-J复赛

惘闻文雀约 33 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 33 分钟

版本

  1. v2当前
  2. v1

CSP-J复赛知识点

模拟

不高兴的津津

C++
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int maxv = -1, p;
    for (int i = 1; i <= 7; i ++ )
    {
        int a, b;
        cin >> a >> b;
        if (a + b > maxv)
        {
            maxv = a + b;
            p = i;
        }
    }

    if (maxv > 8) printf("%d\n", p);
    else puts("0");

    return 0;
}

花生采摘

C++
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;

const int N = 30;

int n, m, k;
int g[N][N];

PII get_max()
{
    PII r = {0, 0};

    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= m; j ++ )
            if (g[r.first][r.second] < g[i][j])
                r = {i, j};

    return r;
}

int main()
{
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= m; j ++ )
            cin >> g[i][j];

    auto t = get_max();
    if (t.first * 2 + 1 > k) puts("0");
    else
    {
        int res = g[t.first][t.second];
        k -= t.first + 1;
        g[t.first][t.second] = 0;

        while (true)
        {
            auto r = get_max();
            int d = abs(r.first - t.first) + abs(r.second - t.second);

            if (d + r.first + 1 > k) break;
            if (!g[r.first][r.second]) break;
            res += g[r.first][r.second];
            g[r.first][r.second] = 0;
            k -= d + 1;
            t = r;
        }

        printf("%d\n", res);
    }

    return 0;
}

陶陶摘苹果

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int h[10];
    for (int i = 0; i < 10; i ++ ) cin >> h[i];
    int x;
    cin >> x;

    int res = 0;
    for (int i = 0; i < 10; i ++ )
        if (x + 30 >= h[i])
            res ++ ;

    cout << res << endl;
    return 0;
}

校门外的树

C++

接水问题

C++
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10010, M = 110;

int n, m;
int w[N];
int q[M];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i ++ ) scanf("%d", &w[i]);

    for (int i = 0; i < n; i ++ )
    {
        int t = 0;
        for (int j = 0; j < m; j ++ )
            if (q[j] < q[t])
                t = j;

        q[t] += w[i];
    }

    int res = 0;
    for (int i = 0; i < m; i ++ ) res = max(res, q[i]);

    cout << res << endl;

    return 0;
}

成绩

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    cout << a * 0.2 + b * 0.3 + c * 0.5 << endl;

    return 0;
}

公交换乘

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 100010;

int n;
struct Coupon
{
    int p, t;
}q[N];
bool st[N];

int main()
{
    scanf("%d", &n);
    int hh = 0, tt = -1;

    int res = 0;
    while (n -- )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        if (!a) res += b, q[ ++ tt] = {b, c};
        else
        {
            while (hh <= tt && q[hh].t < c - 45) hh ++ ;  // 删除过期优惠券
            for (int i = hh; i <= tt; i ++ )
                if (!st[i] && q[i].p >= b)
                {
                    st[i] = true;
                    b = 0;
                    break;
                }
            res += b;
        }
    }

    printf("%d\n", res);
    return 0;
}

排序

明明的随机数

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 110;

int n;
int q[N];

int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++ ) cin >> q[i];
    sort(q, q + n);
    int k = unique(q, q + n) - q;
    cout << k << endl;
    for (int i = 0; i < k; i ++ )
        cout << q[i] << ' ';
    return 0;
}

奖学金

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 310;

int n;
struct Person
{
    int id, sum, a, b, c;
    bool operator< (const Person &t) const
    {
        if (sum != t.sum) return sum > t.sum;
        if (a != t.a) return a > t.a;
        return id < t.id;
    }
}p[N];

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        p[i] = {i + 1, a + b + c, a, b, c};
    }
    sort(p, p + n);
    for (int i = 0; i < 5; i ++ )
        printf("%d %d\n", p[i].id, p[i].sum);

    return 0;
}

分数线划定

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 5010;

int n, m;
struct Person
{
    int id, score;
    bool operator< (const Person& t) const
    {
        if (score != t.score) return score > t.score;
        return id < t.id;
    }
}p[N];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i ++ ) scanf("%d%d", &p[i].id, &p[i].score);
    sort(p, p + n);
    int k = 1.5 * m;
    while (k < n && p[k - 1].score == p[k].score) k ++ ;
    printf("%d %d\n", p[k - 1].score, k);
    for (int i = 0; i < k; i ++ )
        printf("%d %d\n", p[i].id, p[i].score);
    return 0;
}

瑞士轮

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 200010;

int n, m, Q;
int q[N], q0[N], q1[N], s[N], w[N];

int main()
{
    scanf("%d%d%d", &n, &m, &Q);
    n *= 2;
    for (int i = 0; i < n; i ++ ) scanf("%d", &s[i]);
    for (int i = 0; i < n; i ++ ) scanf("%d", &w[i]);
    for (int i = 0; i < n; i ++ ) q[i] = i;

    sort(q, q + n, [&](int a, int b) {
        if (s[a] != s[b]) return s[a] > s[b];
        return a < b;
    });

    while (m -- )
    {
        int x = 0, y = 0;
        for (int i = 0; i < n; i += 2)
        {
            int a = q[i], b = q[i + 1];
            if (w[a] > w[b])
            {
                s[a] ++ ;
                q0[x ++ ] = b;
                q1[y ++ ] = a;
            }
            else
            {
                s[b] ++ ;
                q0[x ++ ] = a;
                q1[y ++ ] = b;
            }
        }

        x = 0, y = 0;
        int k = 0;
        while (x < n / 2 && y < n / 2)
        {
            int a = q0[x], b = q1[y];
            if (s[a] > s[b] || s[a] == s[b] && a < b) q[k ++ ] = q0[x ++ ];
            else q[k ++ ] = q1[y ++ ];
        }
        while (x < n / 2) q[k ++ ] = q0[x ++ ];
        while (y < n / 2) q[k ++ ] = q1[y ++ ];
    }

    printf("%d\n", q[Q - 1] + 1);

    return 0;
}

字符串处理

乒乓球

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

void work(string str, int score)
{
    int a = 0, b = 0;
    for (auto c: str)
    {
        if (c == 'E') break;
        if (c == 'W') a ++ ;
        else b ++ ;
        if (max(a, b) >= score && abs(a - b) >= 2)
        {
            printf("%d:%d\n", a, b);
            a = 0, b = 0;
        }
    }
    printf("%d:%d\n", a, b);
}

int main()
{
    string str, line;
    while (cin >> line) str += line;
    work(str, 11);
    puts("");
    work(str, 21);
    return 0;
}

ISBN号码

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    string str;
    int sum = 0;
    cin >> str;

    for (int i = 0, j = 1; i + 1 < str.size(); i ++ )
        if (str[i] != '-')
        {
            sum += (str[i] - '0') * j;
            j ++ ;
        }
    sum %= 11;
    char c = sum + '0';
    if (sum == 10) c = 'X';
    if (c == str.back()) puts("Right");
    else
    {
        str.back() = c;
        cout << str << endl;
    }

    return 0;
}

立体图

C++
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 500;

int n, m;
char box[6][8] = {
    "..+---+",
    "./   /|",
    "+---+ |",
    "|   | +",
    "|   |/.",
    "+---+.."
};
char g[N][N];
int h[N][N];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            scanf("%d", &h[i][j]);

    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            g[i][j] = '.';

    int up = N, right = 0;
    for (int x = 0; x < n; x ++ )
        for (int y = 0; y < m; y ++ )
            for (int z = 0; z < h[x][y]; z++)
            {
                int X = 499 - 2 * (n - 1 - x) - 3 * z;
                int Y = 2 * (n - 1 - x) + 4 * y;
                up = min(up, X - 5);
                right = max(right, Y + 6);
                for (int a = 0; a < 6; a++)
                    for (int b = 0; b < 7; b++)
                        if (box[a][b] != '.')
                            g[X - 5 + a][Y + b] = box[a][b];
            }

    for (int i = up; i < N; i++)
    {
        for (int j = 0; j <= right; j++) printf("%c", g[i][j]);
        puts("");
    }

    return 0;
}

枚举

数字统计

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int l, r;
    cin >> l >> r;
    int res = 0;
    for (int i = l; i <= r; i ++ )
        for (auto c: to_string(i))
            if (c == '2')
                res ++ ;
    cout << res << endl;

    return 0;
}

寻宝

C++
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10010, M = 110, mod = 20123;

int n, m, k;
bool st[N][M];
int x[N][M];

int main()
{
    scanf("%d%d", &n, &m);

    int res = 0;
    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < m; j ++ )
            scanf("%d%d", &st[i][j], &x[i][j]);

    scanf("%d", &k);
    for (int i = 0; i < n; i ++ )
    {
        int s = 0;
        for (int j = 0; j < m; j ++ ) s += st[i][j];
        int t = x[i][k];
        res = (res + t) % mod;
        t %= s;
        if (!t) t = s;

        for (int j = k; ;j = (j + 1) % m)
        {
            if (st[i][j])
            {
                if (-- t == 0)
                {
                    k = j;
                    break;
                }
            }
        }
    }

    printf("%d\n", res);
    return 0;
}

比例简化

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

double get(int a, int b)
{
    return (double)a / b;
}

int main()
{
    int A, B, L;
    cin >> A >> B >> L;

    int a = L, b = 1;
    for (int i = 1; i <= L; i ++ )
        for (int j = 1; j <= L; j ++ )
            if (get(i, j) >= get(A, B) && get(i, j) < get(a, b))
                a = i, b = j;
    cout << a << ' ' << b << endl;

    return 0;
}

金币

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

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

    int res = 0;
    for (int i = 1, k = 0; k < n; i ++ )
        for (int j = 0; j < i && k < n; j ++, k ++ )
            res += i;

    cout << res << endl;
    return 0;
}

贪心

火星人

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 10010;

int n, m;
int q[N];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
    while (m -- )
    {
        int k = n - 1;
        while (q[k - 1] > q[k]) k -- ;
        int t = k - 1;
        while (k + 1 < n && q[k + 1] > q[t]) k ++ ;
        swap(q[t], q[k]);
        reverse(q + t + 1, q + n);
    }

    for (int i = 0; i < n; i ++ ) printf("%d ", q[i]);
    return 0;
}

Jam的计数法

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int a, b, n;
    string s;
    cin >> a >> b >> n >> s;
    a --, b -- ;

    for (int i = 0; i < 5; i ++ )
    {
        int k = n - 1;
        while (k >= 0 && b - (s[k] - 'a') < n - k) k -- ;
        if (k < 0) break;
        for (int j = k, c = s[k] + 1; j < n; j ++, c ++ )
            s[j] = c;
        cout << s << endl;
    }

    return 0;
}

纪念品分组

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 30010;

int n, m;
int w[N];

int main()
{
    scanf("%d%d", &m, &n);
    for (int i = 0; i < n; i ++ ) scanf("%d", &w[i]);
    sort(w, w + n);

    int res = 0;
    for (int j = n - 1, i = 0; i <= j; j -- )
    {
        if (w[i] + w[j] <= m) i ++ ;
        res ++ ;
    }

    printf("%d\n", res);
    return 0;
}

三国游戏

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 510;

int n;
int w[N][N];

int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++ )
        for (int j = i + 1; j < n; j ++ )
        {
            cin >> w[i][j];
            w[j][i] = w[i][j];
        }

    int res = 0;
    for (int i = 0; i < n; i ++ )
    {
        int x = 0, y = 0;
        for (int j = 0; j < n; j ++ )
            if (w[i][j] > x) y = x, x = w[i][j];
            else y = max(y, w[i][j]);
        res = max(res, y);
    }

    cout << 1 << endl;
    cout << res << endl;
    return 0;
}

推销员

C++
#include <iostream>
#include <cstring>
#include <algorithm>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

const int N = 100010;

int n;
PII q[N];
int f[N], g[N], h[N];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &q[i].y);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &q[i].x);
    sort(q + 1, q + n + 1, greater<PII>());

    for (int i = 1; i <= n; i ++ )
    {
        f[i] = f[i - 1] + q[i].x;
        g[i] = max(g[i - 1], q[i].y);
    }
    for (int i = n; i; i -- ) h[i] = max(h[i + 1], q[i].x + q[i].y * 2);

    for (int i = 1; i <= n; i ++ )
        printf("%d\n", max(f[i] + g[i] * 2, f[i - 1] + h[i]));
    return 0;
}

数学

麦森数

C++
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int N = 500;

void mul(int c[], int a[], int b[])
{
    static int tmp[N];
    memset(tmp, 0, sizeof tmp);
    for (int i = 0; i < N; i ++ )
        for (int j = 0; j < N; j ++ )
            if (i + j < N)
                tmp[i + j] += a[i] * b[j];

    for (int i = 0, t = 0; i < N; i ++ )
    {
        t += tmp[i];
        c[i] = t % 10;
        t /= 10;
    }
}

void qmi(int k)
{
    int res[N] = {1};
    int a[N] = {2};

    while (k)
    {
        if (k & 1) mul(res, res, a);
        mul(a, a, a);
        k >>= 1;
    }

    res[0] -- ;
    for (int i = 0, k = N - 1; i < 10; i ++ )
    {
        for (int j = 0; j < 50; j ++, k -- )
            printf("%d", res[k]);
        puts("");
    }
}

int main()
{
    int p;
    cin >> p;
    cout << (int)(p * log10(2)) + 1 << endl;

    qmi(p);

    return 0;
}

Hanoi双塔问题

C++
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

vector<int> add(vector<int> A, vector<int> B)  // C = A + B, A >= 0, B >= 0
{
    if (A.size() < B.size()) return add(B, A);

    vector<int> C;
    int t = 0;
    for (int i = 0; i < A.size(); i ++ )
    {
        t += A[i];
        if (i < B.size()) t += B[i];
        C.push_back(t % 10);
        t /= 10;
    }

    if (t) C.push_back(t);
    return C;
}

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

    vector<int> a{2};
    for (int i = 0; i < n - 1; i ++ )
        a = add(add(a, a), {2});

    for (int i = a.size() - 1; i >= 0; i -- )
        cout << a[i];

    return 0;
}

魔法阵

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 15010, M = 40010;

int n, m;
int cnt[N];
int a[N], b[N], c[N], d[N], x[M];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; i ++ )
    {
        scanf("%d", &x[i]);
        cnt[x[i]] ++ ;
    }

    for (int t = 1; t * 9 + 2 <= n; t ++ )
    {
        int sum = 0;
        for (int D = 1 + 9 * t + 1; D <= n; D ++ )
        {
            int C = D - t;
            int A = D - 9 * t - 1;
            int B = A + t * 2;
            sum += cnt[A] * cnt[B];
            c[C] += sum * cnt[D];
            d[D] += sum * cnt[C];
        }

        sum = 0;
        for (int A = n - 9 * t - 1; A >= 1; A -- )
        {
            int B = A + t * 2;
            int D = A + 9 * t + 1;
            int C = D - t;
            sum += cnt[C] * cnt[D];
            a[A] += sum * cnt[B];
            b[B] += sum * cnt[A];
        }
    }

    for (int i = 0; i < m; i ++ )
        printf("%d %d %d %d\n", a[x[i]], b[x[i]], c[x[i]], d[x[i]]);

    return 0;
}

分糖果

C++
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int n, l, r;
    cin >> n >> l >> r;
    if (l / n == r / n)
        cout << r % n << endl;
    else
        cout << n - 1 << endl;

    return 0;
}
原文