Codeforces Round 969 (Div. 2) 题ABC详细题解,包含(C++,Python语言描述)

前言:

首先让我们恭喜tourist创造历史

他是第一,他又是第一,他总是第一,第一个codefores上4000分的,创造一个新的段位:Tourist,他的名字就是一个新的段位,他就是最高的山,最长的河

本文为Codeforces Round 969 (Div. 2)题ABC的详细题解,包含题目大意和解题思路,C++,Python两种语言编写,觉得有帮助或者写的不错可以点个赞

目录

题A:

题目大意和解题思路:

代码(C++):

代码(Python):

题B:

题目大意和解题思路:

代码(C++):

代码(Python):

题C:

题目大意和解题思路:

代码(C++):

代码(Python):


题A:

Problem - A - Codeforces

题目大意和解题思路:

题目意思就是说:在给定范围内的整数中,我们可以找到多少组三个数,这三个数两两之间的最大公约数都是1。每执行一次操作,就会从集合中移除三个数,然后找出最大的操作次数

取两个相邻的奇数,然后夹一个偶数即可,这样就可以保证操作次数最大

先统计奇数的数量,然后除以二即可

相邻的两个奇数,中间夹一个偶数,他们的最大公约数一定都是1

代码(C++):

cpp 复制代码
int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    int tt;
    std::cin >> tt;
    while (tt--) {
        int l, r;
        std::cin >> l >> r;

        int cnt = 0;
        for (int i = l; i <= r; i++) {
            if (i % 2 != 0) {
                cnt++;
            }
        }

        std::cout << cnt / 2 << "\n";
    }
}

代码(Python):

python 复制代码
def main():
    tt = int(input())
    res = []

    for _ in range(tt):
        l, r = map(int, input().split())

        cnt = 0
        for i in range(l, r + 1):
            if i % 2 != 0:
                cnt += 1
        
        res.append(cnt // 2)
        
    for r in res:
        print(r)

题B:

Problem - B - Codeforces

题目大意和解题思路:

题目就是给一个数组,然后给两种操作,分别是 + l r 和 - l r

选择数组中满足小于等于l 且大于等于r 的元素加一(+),或者减一(-)

最后输出数组中的最大值

我觉得B比A简单

关注数组中的最大值即可,如果最大值满足在[l, r]之间就对其进行加减操作

Q:为什么只用关注数组中的最大值?

A:可以这么考虑,假设有一个比最大值小的一个数x,无论l, r是多少,这个x最多只能等于最大值

代码(C++):

cpp 复制代码
int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    int tt;
    std::cin >> tt;
    while (tt--) {
        int n, m;
        std::cin >> n >> m;
        int res = 0;
        
        for (int i = 0; i < n; i++) {
            int x;
            std::cin >> x;
            res = std::max(res, x);
        }

        while (m--) {
            char c;
            int l, r;
            std::cin >> c >> l >> r;
            if (l <= res && res <= r) {
                if (c == '-') {
                    res--;
                } else {
                    res++;
                }
            }
            std::cout << res << " ";
        }
        std::cout << "\n";
    }
}

代码(Python):

python 复制代码
def main():
    tt = int(input())
    res = []

    for _ in range(tt):
        n, m = map(int, input().split())
        x = max(list(map(int, input().split())))
        arr = []
        
        for _ in range(m):
            c, l, r = input().split()
            l, r = int(l), int(r)
            if l <= x <= r:
                if c == "-":
                    x -= 1
                else:
                    x += 1
            arr.append(x)

        res.append(arr)

    for r in res:
        print(*r)

题C:

Problem - C - Codeforces

题目大意和解题思路:

给你一个数组,两个数字a和b,你可以对数组中任意一个元素操作任意次:加a或者加b

然后定义一个x = 数组最大值减最小值

现在进行任意操作,使得这个x最小,求出x的最小值

数学思维题,每次任意增加a和b,那么可以理解成每次增加的值,一定是gcd(a, b)的倍数h

要使得最大值和最小值的差值最小,每次增加h的若干倍数,那么只需要关注数组元素对h取余数的情况,余数相差最小的那两个数作为"最大值"和"最小值"

可以对数组每个数字取余数,然后排序,相邻两个数字之间的相差最小,那么这两个相邻数字就是要求的"最大值"和"最小值",对其中一个数字加上h变成"最大值"即可

代码(C++):

cpp 复制代码
int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}
int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    int tt;
    std::cin >> tt;
    while (tt--) {
        int n, a, b;
        std::cin >> n >> a >> b;
        std::vector<int> A(n);
        int h = gcd(a, b);
        for (int i = 0; i < n; i++) {
            std::cin >> A[i];
            A[i] %= h;
        }

        std::sort(A.begin(), A.end());
        long long res = A[n - 1] - A[0];
        for (int i = 0; i < n - 1; i++) {
            A[i] += h;
            res = std::min(res, (long long)(A[i] - A[i+1]));
        }

        std::cout << res << "\n";
    }
}

代码(Python):

python 复制代码
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a
def main():
    tt = int(input())
    result = []

    for _ in range(tt):
        n, a, b = map(int, input().split())
        h = gcd(a, b)
        A = [int(x) % h for x in input().split()]

        A.sort()
        res = A[n - 1] - A[0]
        for i in range(0, len(A) - 1):
            A[i] += h
            res = min(res, A[i] - A[i + 1])
        result.append(res)

    for res in result:
        print(res)
相关推荐
韩立学长2 分钟前
【开题答辩实录分享】以《基于Python的新能源汽车管理系统的设计与实现》为例进行答辩实录分享
python·新能源汽车
charlie11451419123 分钟前
Windows 10 系统编程——线程专题1
c++·windows·学习·线程
夏鹏今天学习了吗31 分钟前
【LeetCode热题100(35/100)】LRU 缓存
算法·leetcode·缓存
Pocker_Spades_A44 分钟前
中秋与代码共舞:用Python、JS、Java打造你的专属中秋技术盛宴
python
拾光Ծ1 小时前
【C++】STL有序关联容器的双生花:set/multiset 和 map/multimap 使用指南
数据结构·c++·算法
梁萌1 小时前
自动化测试框架playwright使用
自动化测试·python·ui自动化·playwright
Python×CATIA工业智造1 小时前
Python回调函数中携带额外状态的完整指南:从基础到高级实践
python·pycharm
害恶细君1 小时前
【超详细】使用conda配置python的开发环境
开发语言·python·jupyter·pycharm·conda·ipython
java1234_小锋1 小时前
TensorFlow2 Python深度学习 - TensorFlow2框架入门 - 变量(Variable)的定义与操作
python·深度学习·tensorflow·tensorflow2
澄澈i2 小时前
设计模式学习[20]---桥接模式
c++·学习·设计模式·桥接模式