AtCoder Beginner Contest 397 A - D题解

Tasks - OMRON Corporation Programming Contest 2025 (AtCoder Beginner Contest 397)

本文为 AtCoder Beginner Contest 397 A - D题解

题目A:

代码(C++):

cpp 复制代码
#include <bits/stdc++.h>

int main() {
    double n;
    std::cin >> n;

    if (n >= 38.0) {
        std::cout << 1;
    } else if (n >= 37.5) {
        std::cout << 2;
    } else {
        std::cout << 3;
    }
}

题目B: 简单贪心,一次遍历

B - Ticket Gate Log

代码(C++):

cpp 复制代码
#include <bits/stdc++.h>

int main() {
    std::string s;
    std::cin >> s;

    int ans = 0;
    bool f = true;
    for (int i = 0; i < s.size(); i++) {
        if (f) {
            if (i % 2 == 0 && s[i] != 'i' || i % 2 == 1 && s[i] != 'o') {
                f = !f;
                ans++;
            }
        } else {
            if (i % 2 == 1 && s[i] != 'i' || i % 2 == 0 && s[i] != 'o') {
                f = !f;
                ans++;
            }
        }
    }

    if ((ans + s.size()) % 2 != 0) {
        ans++;
    }
    std::cout << ans;
}

题目C: 哈希表

C - Variety Split Easy

题目大意:

给你一个数组,把数组分成两个部分,使得数组左边所含有的不同数字个数 + 右边所含有的不同数字个数最大

解题思路:

用两个哈希表mp1, mp2进行模拟即可,mp1先把所有数字添加进去,然后从左到右遍历,mp1减去当前数字,mp2加上当前数字,然后更新答案即可

代码(C++):

cpp 复制代码
#include <bits/stdc++.h>

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

    std::vector<int> a(n);
    std::set<int> st;
    std::map<int, int> mp;
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
        mp[a[i]]++;
    }

    int ans = 0;

    for (int i = 0; i < n; i++) {
        mp[a[i]]--;
        if (mp[a[i]] == 0) {
            mp.erase(a[i]);
        }
        st.insert(a[i]);
        ans = std::max(ans, int(st.size() + mp.size()));
    }

    std::cout << ans;
}

题目D: 数学推导,枚举优化

题目大意:

给定一个数字n,请你找出两个正整数x, y,满足x * x * x - y * y * y = n

解题思路:

此题我参考jiangly的写法,给出如下详细推导证明:

由上述推导可知,我们可以进行以下枚举:

时间复杂度为,可以满足题目要求

cpp 复制代码
for (i64 d = 1; d * d * d <= n; d++) {
    
}

现在问题就变成了,如何用这个d和n找出y和x,可以参考下面推导:

​​​、​​​​​

字写的有点丑,见谅...

代码(C++):

cpp 复制代码
#include <bits/stdc++.h>

using i64 = long long;

int main() {
    i64 n;
    std::cin >> n;

    for (i64 d = 1; d * d * d <= n; d++) {
        if (n % d != 0) {
            continue;
        }
        i64 p = n / d - d * d;
        if (p % 3 != 0) {
            continue;
        }
        p /= 3;

        i64 y = (std::sqrt(d * d + 4 * p) - d) / 2 + 0.5;
        if (y <= 0) {
            continue;
        }
        i64 x = y + d;
        if (__int128(x) * x * x - __int128(y) * y * y == n) {
            std::cout << x << " " << y;
            return 0;
        }
    }

    std::cout << -1;
}
相关推荐
爱数模的小驴1 小时前
2025 年“认证杯”数学中国数学建模网络挑战赛 C题 化工厂生产流程的预测和控制
深度学习·算法·计算机视觉
码农新猿类2 小时前
服务器本地搭建
linux·网络·c++
GOTXX2 小时前
【Qt】Qt Creator开发基础:项目创建、界面解析与核心概念入门
开发语言·数据库·c++·qt·图形渲染·图形化界面·qt新手入门
徐行1102 小时前
C++核心机制-this 指针传递与内存布局分析
开发语言·c++
序属秋秋秋3 小时前
算法基础_数据结构【单链表 + 双链表 + 栈 + 队列 + 单调栈 + 单调队列】
c语言·数据结构·c++·算法
apcipot_rain4 小时前
【密码学——基础理论与应用】李子臣编著 第五章 序列密码 课后习题
算法·密码学
不要不开心了4 小时前
sparkcore编程算子
pytorch·分布式·算法·pygame
88号技师4 小时前
【2024年最新IEEE Trans】模糊斜率熵Fuzzy Slope entropy及5种多尺度,应用于状态识别、故障诊断!
人工智能·算法·matlab·时序分析·故障诊断·信息熵·特征提取
mldl_4 小时前
(个人题解)第十六届蓝桥杯大赛软件赛省赛C/C++ 研究生组
c语言·c++·蓝桥杯
一个小白14 小时前
C++ 用红黑树封装map/set
java·数据库·c++