Codeforces Round 970 (Div. 3)(A~H)

news2024/9/23 13:17:39

​​​​​题目链接​​​​​​​​​​​​​​​​​​​​​

A

当 a 为奇数的时候,无论如何配对都无法将最后一个 1 减去;
当 a 为偶数的时候,b 也偶数,自然可以内部通过加减操作变成 0;当 b 为奇数的时候,只有当 a = 0 的时候,无法变成 0,因为 a 为偶数,且不为 0,那么一定可以拿出两个 1 和 b 中的一个配对,然后 a 和 b 都为偶数了。

代码

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
using LL = long long;

void slove()
{
    int a, b; cin >> a >> b;
    if (a & 1) cout << "No" << endl;
    else 
        if (a == 0 && b & 1) cout << "NO" << endl;
        else cout << "YES" << endl;
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t; cin >> t;
    while (t--) slove();
    return 0;
}

B

首先判断长度 n 是否可以构成一个正方形;字符串映射为一个二维矩阵,外围为 1,中间全都为 0,那么可以暴力枚举这个二维矩阵,判断该单元格是否为最外围的格子的依据是:判断 i、j 是否等于 1 或 a (a 为这个正方形的边长,设字符串下标从 1 开始)

代码

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
using LL = long long;

void slove()
{
    int n; cin >> n;
    string str; cin >> str;
    str = " " + str;
    int len = sqrt(n);
    if (len * len != n) { cout << "NO" << endl; return; }
    
    bool flag = true;
    for (int i = 1; i <= len; i++)
    {
        for (int j = 1; j <= len; j++)
        {
            if (i == 1 || i == len || j == 1 || j == len)
            {
                if (str[(i - 1) * len + j] != '1')
                    flag = false;
            }
                
            else 
                if (str[(i - 1) * len + j] != '0')
                    flag = false;
        }
    }
    
    if (flag) cout << "YES" << endl;
    else cout << "NO" << endl;
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t; cin >> t;
    while (t--) slove();
    return 0;
}

C

为了使得数组长度更长,数组中的元素值需要尽可能小。
第一个条件是数组中的元素需要升序,第二个条件是相邻元素的差值也得升序。
为了使长度最长,第一个元素自然等于 L,差值也需要尽可能小且升序,那么只能是公差为 1 的等差数列。
设长度为 n,那么等差数列的项数为 n - 1,且 a1 = L,an <= R。

 可以使用二分求解 n,当然也可以根据数据范围暴力枚举 n。

二分代码

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
using LL = long long;

void slove()
{
    int a, b; cin >> a >> b;
    int l = 1, r = 1e9;
    while (l < r)
    {
        int mid = l + r + 1>> 1;
        if ((LL) mid * (mid - 1) <= 2 * (b - a)) l = mid;
        else r = mid - 1;
    }
    
    cout << l << endl;
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t; cin >> t;
    while (t--) slove();
    return 0;
}

暴力代码

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
using namespace std;
using LL = long long;

void slove()
{
    LL l, r; cin >> l >> r;
    LL y = 2 * (r - l);
    LL x = sqrt(y);
    
    LL res = 0;
    for (int i = x; i <= 1e5; i++)
        if ((LL) i * (i - 1) <= y)
            res = i;
    cout << res << endl;
}  

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t; cin >> t;
    while (t--) slove();
    return 0;
}

D

对于任意 i,可以到达 a[i] 位置,记录 i 可以到达的位置,将这些位置看作一个集合,以集合中任意一个为起点,且可以到达集合中任意一个位置,那么也可以得到集合中所有的黑色。
使用并查集,使用一个 cnt 数组记录以 i 为根节点的集合中有多少黑色,每次在合并节点的时候维护 cnt 数组
注意:下标需要从 1 开始

代码

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
using LL = long long;

const int N = 2e5 + 10;
int p[N], cnt[N];

int find(int x)
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

void slove()
{
    memset(cnt, 0, sizeof cnt);
    int n; cin >> n;
    for (int i = 0; i <= n; i++) p[i] = i;
    vector<int> a(n + 10);
    
    for (int i = 1; i <= n; i++) cin >> a[i];
    string str; cin >> str;
    str = " " + str;
    
    for (int i = 1; i <= n; i++)
        cnt[i] = str[i] == '0';
    
    for (int i = 1; i <= n; i++)
    {
        int pa = find(i), pb = find(a[i]);
        if (pa != pb)
        {
            p[pa] = pb;
            cnt[pb] += cnt[pa];
        }
    }
    
    for (int i = 1; i <= n; i++)
    {
        int pa = find(i);
        cout << cnt[pa] << " ";
    }
    cout << endl;
    
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t; cin >> t;
    while (t--) slove();
    return 0;
}

E

最多只能执行删除操作一次,且最后字符串长度必须为偶数,所以删除操作在长度为奇数时执行。
当长度为偶数时,不执行删除操作。那么不需要替换的是出现次数最多的字母。
当长度为奇数时,必须执行删除操作。  枚举删除的位置 i,i 之前的奇偶不需要转变, i 之后的奇数位置变为偶数位置,偶数位置变为奇数位置。
 

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
using LL = long long;


void slove()
{
    int n; cin >> n;
    string str; cin >> str;
    
    if (n & 1)
    {
        vector<vector<int>> pre(2, vector<int>(26));
        vector<vector<int>> suf(2, vector<int>(26));
        for (int i = n - 1; i >= 0; i--)
            suf[i % 2][str[i] - 'a']++;
            
        int ans = 1e9;
        for (int i = 0; i < n; i++)
        {
            suf[i % 2][str[i] - 'a']--;
            int res = n;
            for (int k = 0; k <= 1; k++)
            {
                int maxv = 0;
                for (int j = 0; j < 26; j++)
                {
                    maxv = max(maxv, pre[k][j] + suf[1 - k][j]);  // 后面奇偶互换
                }
                res -= maxv;
            }
            pre[i % 2][str[i] - 'a']++;
            ans = min(ans, res);
        }
        cout << ans << endl;
    }
    else
    {
        vector<vector<int>> a(2, vector<int>(26));
        for (int i = 0; i < n; i++)
            a[i % 2][str[i] - 'a']++;
            
        int res = n;
        for (int i = 0; i <= 1; i++)
        {
            int maxv = 0;
            for (int j = 0; j < 26; j++)
                maxv = max(maxv, a[i][j]);
            res -= maxv;
        }
        cout << res << endl;
    }
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t; cin >> t;
    while (t--) slove();
    return 0;
}

F

这题就是一个逆元的使用,主要溢出就行

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
using LL = long long;
const int MOD = 1e9 + 7;

LL inv(LL a)
{
    LL res = 1;
    int k = MOD - 2;
    while (k)
    {
        if (k & 1) res = res * a % MOD;
        a = a * a % MOD;
        k >>= 1;
    }
    return res;
}

void slove()
{
    int n; cin >> n;
    LL m = 0;
    vector<int> a(n + 10);
    for (int i = 0; i < n; i++) 
    {
        cin >> a[i];
        m += a[i];
    }
    
    LL res = 0;
    for (int i = 0; i < n; i++)
    {
        m -= a[i];
        res = (res + (m % MOD)* (LL) a[i] % MOD) % MOD;
    }
    
    LL Q = ((LL) n * (n - 1) / 2) % MOD;
    cout << (res * inv(Q) % MOD) << endl;
}

int main()
{
    int t; cin >> t;
    while (t--) slove();
    return 0;
}

G

进行任意次操作之后,最小可以得到一个由最大公约数 g 的整数倍数组。
为了求得最小mex,需要尽量将数组中的元素不重复的最小,那么就是由 g 构成的整数倍数组
相邻两个元素相差 g - 1 个元素,那么可以枚举得到最小 mek。
需要特别处理只有一个元素的情况。

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
using LL = long long;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

int main()
{
    int t; cin >> t;
    while (t--) 
    {
        int n, k; cin >> n >> k;
        vector<LL> a(n + 10, 0);
        int g = 0;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            g = gcd(g, (int) a[i]);
        }
        
        if (n == 1)
        {
            if (a[0] >= k)
                cout << k - 1 << endl;
            else 
            {
                cout << k << endl;
            }
            continue;
        }
        
        for (int i = 0; i < n; i++)
            a[i] = (LL) i * g;

        LL res = 0;
        for (int i = 1; i < n; i++)
        {
            if (k <= g - 1) break;
            
            k -= g - 1;
            res = a[i];
        }
        cout << res + k << endl;
        
    }
    return 0;
}

H

对于任意一个数组中的大于 x 元素,可以执行任意次减 x 的操作,那么最后的结果是 mod(x) 的值。为了使中位数最小,自然每一个元素都得减 x 直至最小,所以中位数的取值范围 = [0, x - 1]。

二分答案,当 mid 前面的元素个数大于数组长度的一半时,可以缩小 r。更加题目定义,当数组长度为偶数时,中位数取后面那个,那么二分目标值 t = n / 2 + 1。

求某个值前面的元素个数,可以使用前缀和。预处理每一个元素出现的次数,再枚举 n,因为 a[i] <= n;计算出现次数的前缀和。
由于最终出现的次数是基于 mod(x),那么可以分段处理,枚举每一段的起点,也就是说 x 的整数倍。

代码

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
using LL = long long;

void slove()
{
    int n, q; cin >> n >> q;
    vector<int> a(n + 10, 0);
    vector<LL> cnt(n + 10, 0);
    for (int i = 0 ; i < n; i++)
    {
        cin >> a[i];
        cnt[a[i]]++;
    }
    for (int i = 1; i <= n; i++)
    {
        cnt[i] += cnt[i - 1];
    }
    
    vector<int> res(n + 10, 0);
    
    for (int x = 1; x <= n; x++)
    {
        int l = 0, r = x;
        while (l < r)
        {
            int mid = l + r >> 1;
            LL c = cnt[mid];
            
            for (int k = 1; k * x <= n; k++)
            {
                c += cnt[min(k * x + mid, n)] - cnt[k * x - 1];
            }
            int tmp = n / 2 + 1;
            if (c < tmp) l = mid + 1;
            else r = mid;
        }
        res[x] = r;
    }
    
    while (q--)
    {
        int x; cin >> x;
        cout << res[x] << " ";
    }
    cout << endl;
    
    
}

int main()
{
    int t; cin >> t;
    while (t--) slove();
    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2105570.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

观察者设计模式详解

观察者设计模式详解 文章目录 观察者设计模式详解一、定义二、观察者模式的结构三、特点四、应用场景五、实现 一、定义 **观察者设计模式&#xff08;Observer Pattern&#xff09;**是一种行为设计模式&#xff0c;也叫做 “发布-订阅模式”&#xff0c; 它定义了一种一对多…

ArcGIS的8个“合并”功能要分清——矢量:编辑器合并,复制粘贴,工具合并、追加、联合——栅格:镶嵌、镶嵌至新栅

​​​ 点击下方全系列课程学习 点击学习—>ArcGIS全系列实战视频教程——9个单一课程组合系列直播回放 点击学习——>遥感影像综合处理4大遥感软件ArcGISENVIErdaseCognition 今天来看看ArcGIS中的几个涉及“合并”功能的用法。 对矢量&#xff1a; 包括编辑器中的合…

IOS 20 发现界面(UITableView)歌单列表(UICollectionView)实现

发现界面完整效果 本文实现歌单列表效果 文章基于 IOS 19 发现界面&#xff08;UITableView&#xff09;快捷按钮实现 继续实现发现界面歌单列表效果 歌单列表Cell实现 实现流程&#xff1a; 1.创建Cell&#xff0c;及在使用UITableView的Controller控制器上注册Cell&#x…

uni-app应用更新(Android端)

关于app更新&#xff0c;uni-app官方推荐的是 uni-upgrade-center&#xff0c;看了下比较繁琐&#xff0c;因此这里自己实现检查更新并下载安装的逻辑。 1.界面效果 界面中的弹框和 进度条采用了uView 提供的组件 2.检查更新并下载安装 一、版本信息配置在服务端&#xff0c…

【Azure Redis】Redis-CLI连接Redis 6380端口始终遇见 I/O Error

问题描述 使用Redis-cli连接Redis服务&#xff0c;因为工具无法直接支持TLS 6380端口连接&#xff0c;所以需要使用 stunnel 配置TLS/SSL服务。根据文章(Linux VM使用6380端口(SSL方式)连接Azure Redis (redis-cli & stunnel) &#xff1a; https://www.cnblogs.com/luligh…

Python使用turtle画笑脸

import turtle as t t.pensize(5) #设置画笔尺寸 t.color("red","yellow") #设置画笔颜色 t.begin_fill() #开始填充 t.circle(150) #绘制一个半径为100像素的圆 t.end_fill() #结束填充#画眼睛&#xff08;左眼…

Leetcode - 周赛413

目录 一&#xff0c;3274. 检查棋盘方格颜色是否相同 二&#xff0c;3275. 第 K 近障碍物查询 三&#xff0c;3276. 选择矩阵中单元格的最大得分 四&#xff0c;3277. 查询子数组最大异或值 一&#xff0c;3274. 检查棋盘方格颜色是否相同 本题就是找规律&#xff0c;假设白…

x11转发远程图形界面

1、 开一个有vnc的节点 2、 开放所有用户的Xserver权限 xhost 3、X11转发 ssh hlzhang192.168.3.156 -X4、打开远程窗口 paraview在227的界面打开156的图形窗口

uniapp和vue3中使用vConsole在H5中开启移动端调试

uniapp和vue3中使用vConsole在H5中开启移动端调试 1. 安装vconsole npm install vconsole --save2. 在main.js中全局引入 重新启动项目即可

【C++】手搓实现模板类

myTamplate.h #ifndef MYTAMPLATE_H #define MYTAMPLATE_H #include <iostream> using namespace std;template<typename T> class Node {T *data; //数据域int size; //指针域int len;//实际长度 public://无参构造Node():size(10),len(0){data new T[size]…

写的一致性问题之双写模式

文章目录 1、先写mysql&#xff1a;mysql会回滚&#xff0c;而redis不会回滚2、先写redis&#xff1a; 1、先写mysql&#xff1a;mysql会回滚&#xff0c;而redis不会回滚 写入msql成功&#xff0c;写入redis也成功&#xff0c;但是后续事务提交失败&#xff0c;mysql会回滚&a…

Django学习(一)

一、创建django项目 二、修改settings.py里的配置&#xff1a; 1、修改语言和时区&#xff1a; # 语言编码 LANGUAGE_CODE zh-hansTIME_ZONE UTCUSE_I18N True# 不用时区 USE_TZ False 2、配置数据库&#xff1a; DATABASES {default: {ENGINE: django.db.backends.m…

Python中的self有什么作用

你是否曾经好奇过,为什么Python类中的方法总是有一个神秘的self参数?为什么有时它似乎可有可无,有时却又不可或缺?今天,让我们一起深入探讨Python中self的奥秘,揭开面向对象编程的神秘面纱! 目录 引言:self的重要性self的本质:实例的引用为什么需要self?self的工作原理self的…

极米科技:走出舒适圈,推动数据架构现代化升级 | OceanBase 《DB大咖说》

《DB 大咖说》第 13 期&#xff0c;邀请到了极米科技软件与创新产品线高级架构师施刘凡来进行分享。 在小红书平台上&#xff0c;“是否应将家里的电视升级为投影仪&#xff1f;”这一话题激发了上百万篇笔记的分享与推荐&#xff0c;反映出年轻群体对投影仪的偏好。随着手机、…

Java MVC

1. MVC模式 1.1. JavaBean JavaBean&#xff1a;符合特定规范的Java类&#xff0c;是一种可重用的组件 特定规范&#xff1a; public, class, 提供无参数构造方法属性private提供public的getter和setter方法 功能分类&#xff1a; 封装数据&#xff1a;数据Bean&#xff0c…

【gtokentool】什么是数字货币?怎么使用?

一、什么是数字货币 数字货币是一种基于密码学原理&#xff0c;独立于传统银行体系运行的电子货币形式。数字货币具有以下特点&#xff1a; 去中心化&#xff1a;数字货币采用去中心化的交易验证方式&#xff0c;不依赖于任何中央机构或政府。安全性高&#xff1a;通过加密算法…

STM32G474之DAC

STM32G474分别使用CORDIC硬件和“math.h”的正弦值&#xff0c;从DAC1和DAC2输出。 1、DAC特点 PA4的附加功能为DAC1_OUT1&#xff0c;无需映射&#xff0c;直接将它配置为模拟功能&#xff0c;就可以使用了。 PA6的附加功能为DAC2_OUT1&#xff0c;无需映射&#xff0c;直接将…

数据结构-栈、队列-详解

数据结构-栈、队列-详解 1.前言2.栈2.1是什么2.2函数实现struct StackStackInitStackDestroyStackPushStackSizeStackEmptyStackTopStackPop 2.3小结 3.队列3.1是什么3.2函数实现struct QueueQueueInitQueueDestroyQueueEmptyQueuePushQueuePopQueueFrontQueueBackQueueSize 3.…

Verilog基础,原码,反码与补码的概念

Verilog模块初认识 1、Verilog模块(Module) Verilog中的module可以看成一个具有输入输出端口的黑盒子&#xff0c;该黑盒子有输入和输出接口(信号)&#xff0c;通过把输入在盒子中执行某些操作来实现某项功能。(类似于C语言中的函数) 图1 模块示意图 1.1 模块描述 图1 所示的…

【408DS算法题】035进阶-17年真题_二叉树转中缀表达式

Index 真题题目分析实现总结 真题题目 请设计一个算法&#xff0c;将给定的表达式树&#xff08;二叉树&#xff09;转换为等价的中缀表达式&#xff08;通过括号反映操作符的计算次序&#xff09;并输出。 例如&#xff0c; 当下列两棵表达式树作为算法的输入时&#xff0c; …