【持续更新】2024牛客寒假算法基础集训营3 题解 | JorbanS

A - 智乃与瞩目狸猫、幸运水母、月宫龙虾

cpp 复制代码
string solve() {
    string a, b; cin >> a >> b;
    if (isupper(a[0])) a[0] += 'a' - 'A';
    if (isupper(b[0])) b[0] += 'a' - 'A';
    return a[0] == b[0] ? yes : no;
}

B - 智乃的数字手串

cpp 复制代码
string solve() {
    cin >> n;
    int cnt = 0;
    for (int i = 1; i <= n; i ++) cin >> a[i], a[i] &= 1, cnt += a[i];
    if (n == 1) return yes;
    if (!cnt || n == cnt) return (n & 1) ? yes : no;
    a[0] = a[n];
    cnt = 0;
    for (int i = 1; i <= n; i ++)
        if (a[i] + a[i - 1] & 1 ^ 1) cnt ++;
    return (cnt & 1) ? yes : no;
}

D - chino's bubble sort and maximum subarray sum(easy version)

cpp 复制代码
int a[N], b[N];
string s;

ll cal() {
    ll res = 0, t = 0;
    for (int i = 0; i < n; i ++) {
        if (b[i] >= 0) t += b[i];
        else {
            res = max(t, res);
            t = max(0ll, t + b[i]);
        }
    }
    res = max(t, res);
    return res;
}

ll solve() {
    cin >> n >> m;
    int c1 = -2e9;
    for (int i = 0; i < n; i ++) {
        cin >> a[i], b[i] = a[i];
        c1 = max(c1, a[i]);
    }
    if (c1 <= 0) return c1;
    ll res = -1e18;
    if (m)
        for (int i = 1; i < n; i ++) {
            swap(b[i], b[i - 1]);
            res = max(res, cal());
            swap(b[i], b[i - 1]);
        }
    else res = cal();
    return res;
}

G - 智乃的比较函数(easy version)

cpp 复制代码
string solve() {
    cin >> n;
    int x, y, z, x1, y1, z1;
    if (n == 1) {
        cin >> x >> y >> z;
        if (x == y && z) return no;
        return yes;
    }
    cin >> x >> y >> z;
    cin >> x1 >> y1 >> z1;
    if (x == x1 && y == y1 && z != z1) return no;
    if (x == y && z) return no;
    if (x1 == y1 && z1) return no;
    if (x == y1 && y == x1 && (z == z1 && z)) return no;
    return yes;
}

H - 智乃的比较函数(normal version)

cpp 复制代码
int a[4][4][2];

string solve() {
    cin >> n;
    for (int i = 1; i <= 3; i ++)
        for (int j = 1; j <= 3; j ++)
            for (int k = 0; k < 2; k ++) a[i][j][k] = 0;
    for (int i = 0; i < n; i ++) {
        int x, y, z; cin >> x >> y >> z;
        a[x][y][z] = 1;
    }
    for (int i = 1; i <= 3; i ++)
        if (a[i][i][1]) return no;
    for (int i = 1; i <= 3; i ++)
        for (int j = 1; j <= 3; j ++)
            if (a[i][j][1] && a[i][j][0]) return no;
    for (int i = 1; i <= 3; i ++)
        for (int j = i + 1; j <= 3; j ++)
            if (a[i][j][1] && a[j][i][1]) return no;
    int b[3] = {1, 2, 3};
    do {
        int x = b[0], y = b[1], z = b[2];
        if (a[x][y][1] && a[y][z][1] && a[z][x][1]) return no;
        if (a[x][y][1] && a[y][z][1] && a[x][z][0]) return no;
        if (a[x][y][0] && a[y][z][0] && a[x][z][1]) return no;
    } while (next_permutation(b, b + 3));
    return yes;
}

J - 智乃的相亲活动

cpp 复制代码
void solve() {
    cin >> n >> m >> k;
    vector<int> a[n + 1], b[m + 1];
    while (k --) {
        int u, v; cin >> u >> v;
        a[u].push_back(v), b[v].push_back(u);
    }
    double na = 0, nb = 0;
    for (int i = 1; i <= n; i ++) {
        double t = 1;
        for (auto j : a[i]) t *= (1 - 1.0 / b[j].size());
        na += 1 - t;
    }
    for (int i = 1; i <= m; i ++) {
        double t = 1;
        for (auto j : b[i]) t *= (1 - 1.0 / a[j].size());
        nb += 1 - t;
    }
    printf("%.8lf %.8lf\n", na, nb);
}

K - 智乃的"黑红树"

cpp 复制代码
int n, a, b;
int l[N], r[N];

void solve() {
    cin >> a >> b;
    if (a & 1 ^ 1 || b & 1 || !a) {
        cout << no << endl;
        return;
    }
    n = a + b;
    for (int i = 1; i <= n; i ++) l[i] = r[i] = -1;
    vector<int> A, B;
    A.push_back(1);
    int idx = 1;
    a --;
    while ((A.size() && b || B.size() && a) {
        while (A.size() && b) {
            l[A[0]] = ++ idx;
            B.push_back(idx);
            r[A[0]] = ++ idx;
            B.push_back(idx);
            A.erase(A.begin());
            b -= 2;
        }
        while (B.size() && a) {
            l[B[0]] = ++ idx;
            A.push_back(idx);
            r[B[0]] = ++ idx;
            A.push_back(idx);
            B.erase(B.begin());
            a -= 2;
        }
    }
    if (a || b) {
        cout << no << endl;
        return;
    }
    cout << yes << endl;
    for (int i = 1; i <= n; i ++) cout << l[i] << ' ' << r[i] << endl;
}

L - 智乃的36倍数(easy version)

cpp 复制代码
int solve() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];
        int res = 0;
    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < n; j ++) {
            if (i == j) continue;
            if ((a[i] * (int)pow(10, to_string(a[j]).size()) + a[j]) % 36 == 0) res ++;
        }
    }
    return res;
}

M - 智乃的36倍数(normal version)

cpp 复制代码
ll solve() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];
    for (int i = 0; i < n; i ++) cnt[a[i] % 36] ++;
    ll res = 0;
    for (int i = 0; i < n; i ++) {
        int x = a[i] % 36;
        int len = to_string(a[i]).size();
        for (int j = 0; j < 36; j ++) {
            int t = j;
            for (int k = 0; k < len; k ++) (t *= 10) %= 36;
            if ((t + x) % 36 == 0) res += max(cnt[j] - (j == x), 0ll);
        }
    }
    return res;
}
相关推荐
九圣残炎18 分钟前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
lulu_gh_yu23 分钟前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
丫头,冲鸭!!!43 分钟前
B树(B-Tree)和B+树(B+ Tree)
笔记·算法
Re.不晚1 小时前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
ULTRA??1 小时前
C加加中的结构化绑定(解包,折叠展开)
开发语言·c++
凌云行者2 小时前
OpenGL入门005——使用Shader类管理着色器
c++·cmake·opengl
凌云行者2 小时前
OpenGL入门006——着色器在纹理混合中的应用
c++·cmake·opengl
为什么这亚子2 小时前
九、Go语言快速入门之map
运维·开发语言·后端·算法·云原生·golang·云计算
2 小时前
开源竞争-数据驱动成长-11/05-大专生的思考
人工智能·笔记·学习·算法·机器学习
~yY…s<#>2 小时前
【刷题17】最小栈、栈的压入弹出、逆波兰表达式
c语言·数据结构·c++·算法·leetcode