Codeforces Round 943 (Div. 3) (A-G1) C++题解

news2024/10/7 2:21:59

目录

比赛链接 : 

A. Maximize?

B. Prefiquence

C. Assembly via Remainders

 D. Permutation Game

E. Cells Arrangement

F. Equal XOR Segments

G1. Division + LCP (easy version)

G2. Division + LCP (hard version)


比赛链接 : 

Dashboard - Codeforces Round 943 (Div. 3) - Codeforces

A. Maximize?

数据范围小,随便搞,遍历即可 : 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;
using namespace std;

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

inline void solve(){
	int x ; cin >> x ;
	int ans  = 0 ,res;
	for(int i=1;i<x;i++){
		int t = gcd(i,x) + i ;
		if(t>ans){
			ans = t ;
			res = i ;	
		}
	}
	cout << res << endl ;
}
 
signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

B. Prefiquence

简单贪心,先设a的遍历下标l为0,在b中遇到一个b[i]==a[l],那么l++,ans++;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
typedef long long LL;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;

using namespace std;

inline void solve() {
	int n , m ; cin >> n >> m ;
	string a , b ; cin >> a >> b ;
	int ans = 0 ;
	int l = 0 ;
	for(int i=0;i<m;i++){
		if(b[i]==a[l]){
			l ++ ;
			ans ++ ;
		}
	}
	cout << ans << endl ;
}

signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while (_--) solve();
    return 0;
}

C. Assembly via Remainders

只要满足a[1]>x[2]的任意一个值,然后后面递推即可 : 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define int long long
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;

using namespace std;

inline void solve(){
	int n ; cin >> n ;
	vector<int> x(n+1) , a(n+1) ;
	for(int i=2;i<=n;i++) cin >> x[i] ;
	a[1] = 1001 ;
	for(int i=2;i<=n;i++) a[i] = a[i-1] + x[i] ;
	for(int i=1;i<=n;i++) cout << a[i] << " " ;
	cout << endl ;
}
 
signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

 D. Permutation Game

首先我们可以先找到a数组的最大值ma ;

  • 如果BodYa和Sasha有可能能够到达ma的话,在k足够大的情况下,一定优先到达ma处,保存这一条路径上的a值 ;
  • 如果不能到达,也就是形成了一个死循环,也可以找出这一条路径的a值,遇到之前遍历过的店,就结束寻找 ,这里用set记录;
  • 路径上的a值分别保存在B和S数组中 ;

那么求出B和S数组的前缀和,分别保存在Bs,Ss数组中;

分别求出可能得到的最大分数 : 

  • 对于BodYa和Sasha两个人,都可以停留在之前获取路径上的任意一点,然后这点下标为i ;
  • 这里分数分两部分,1LL * (k-i) * S[i] 和 1LL * Ss[i],其中对BodYa也一样,遍历求出最大值即可;

.......................

表达能力有限,还是看代码吧,写的很清楚 : 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
typedef long long LL;
const int N = 2e5+10;
using namespace std;

int n , k , pb , ps ;
int p[N] , a[N]  , B[N] , S[N] ;
LL Bs[N] , Ss[N] ;

inline void solve(){
	cin >> n >> k >> pb >> ps ;
	int ma = 0 ;
	for(int i=1;i<=n;i++) cin >> p[i] ;
	for(int i=1;i<=n;i++) {
		cin >> a[i] ;
		if(a[i]>ma) ma = a[i] ;
	}
	int lb = 0 , ls = 0 ;
	set<int> stb , sts ;
	bool tb = false , ts = false ;
	
	while(a[pb]!=ma){
		if(stb.find(pb)!=stb.end()) {
			tb = true ;
			break ;
		}
		B[lb++] = a[pb] ;
		stb.insert(pb);
		pb = p[pb] ;
	}
	if(!tb) B[lb++] = ma ; 
	
	while(a[ps]!=ma){
		if(sts.find(ps)!=sts.end()){
			ts = true ;
			break ;
		}
		S[ls++] = a[ps] ;
		sts.insert(ps) ;
		ps=p[ps];
	}
	if(!ts) S[ls++] = ma ; 
	
	LL mb = 0 , ms = 0 ;// 记录最终分数 
	for(int i=0;i<lb;i++) Bs[i+1] = B[i] + Bs[i] ;
	for(int i=0;i<ls;i++) Ss[i+1] = S[i] + Ss[i] ;
	LL res = 0 ;
	for(int i=0;i<lb;i++){
		if(k>=i+1) res = 1LL * (k-i)*B[i] + 1LL * Bs[i] ;
		else break ;
		mb = max(mb , res) ;
	}
	res = 0 ;
	for(int i=0;i<ls;i++){
		if(k>=i+1) res = 1LL * (k-i) * S[i] + 1LL * Ss[i] ;
		else break ;
		ms = max(ms,res) ;
	}
	if(mb>ms) cout << "Bodya" << endl ;
	else if(mb==ms) cout << "Draw" << endl ;
	else cout << "Sasha" << endl ;
}
 
signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

E. Cells Arrangement

这就是一个guess题,首先可能想到的是全按对角线排布,但是只能够获得全是偶数的H集合;

那么挑出一个(2,2)改为(1,2),就可以获得0,1,2,3,4,...2*(n-1)这么多,且这是最多的;

大概也就是这个样子 : 

 

代码 : 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define int long long
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;

using namespace std;

inline void solve(){
	int n ; cin >> n ;
	cout << "1 1" << endl ;
	cout << "1 2" << endl ;
	for(int i=3;i<=n;i++) cout << i << " " << i << endl ;
	cout << endl ;
}
 
signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

F. Equal XOR Segments

二分 + 位运算

首先要知道的一点是 : 异或(^)是具有前缀和性质的,我们用一个s数组来记录前缀异或和,但是这里a后面也没啥用,直接原地前缀和即可 

[注] : 要求[l,r]的异或 , 直接返回a[r] ^ a[l-1]即可 

如果a[r] ^ a[l-1] == 0,那么一定可以满足题目条件,因为[l,r]中一定存在l<m<r满足a[m]^a[l-1] == a[r]^a[m-1] == v ;

如果a[r]^a[l-1]!=0,设为v,那么如果要满足题目条件,k一定为奇数且一定可以找到三个异或和为v的区间(因为多出来的偶数个会互相抵消) : 

那么题目也就转换成[l,r]能划分为3个异或和为v的区间 : 

这里我们可以用map<int,vector<int>>存下每个前缀异或和的下标;

分两步 : 

1 . 找到最小的i>l使s[i]=s[r],找不到则无解

2 . 找到最小的j>i使s[j]=s[l-1]

如果i<j && j<r则输出yes,否则输出no;

详细请看代码 : 

 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define int long long
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;
using namespace std;

//1
//5 1
//1 1 2 3 0
//1 5

int a[N] ;

inline void solve(){
	int n , q ; cin >> n >> q ;
	map<int,vector<int>> mp ;
	mp[0].push_back(0) ;
	for(int i=1;i<=n;i++) {
		cin >> a[i] ;
		a[i] ^= a[i-1] ;
		mp[a[i]].push_back(i) ;
	}
	while(q--){
		int l , r ; cin >> l >> r ;
		int x = a[r] ^ a[l-1] ;
		if(x==0) {cout << "YES" << endl ; continue ; }
		// v!=0 : k一定为奇数(k>=3),且一定能划3段异或和为v的区间
		// 1 . 找到最小的i>l使s[i]=s[r],找不到则无解
		auto i = lower_bound(mp[a[r]].begin(),mp[a[r]].end(),l) ;
		// cout << *i << endl ;
		if(i==mp[a[r]].end()||*i>=r){
			cout << "No" << endl ;
			continue  ;
		}
		// 2 . 找到最小的j>i使s[j]=s[l-1]
		auto j = upper_bound(mp[a[l-1]].begin(),mp[a[l-1]].end(),*i) ;
		// cout << *j << endl ;
		if(j==mp[a[l-1]].end()){cout << "No" << endl ;continue ;}
		// cout << *i << " " << *j << endl ;
		if(*i<*j &&*j<r) cout << "Yes" << endl ;
		else cout << "No" << endl ;
	} 
	cout << endl ;
	return ;
}
 
signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

G1. Division + LCP (easy version)

二分 + kmp

也可以Hash做

对前缀长度m进行二分,str = s.substr(0,m) ,用kmp判断是否s中满足能够匹配的数量tmp>=k,那么m合法,反之不合法;

这里KMP直接套模板即可 : 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define int long long
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;
using namespace std;

int n,Yss1206,k;
string s ;

bool pd(int m){
	if(m == 0) return 1;
	
	string t = s.substr(0, m);
	vector<int> ne(m + 1, 0);
	
	ne[0] = -1;
	for (int i = 1, j = -1; i < m; i ++ ) {
		while (j >= 0 && t[j + 1] != t[i]) j = ne[j];
		if (t[j + 1] == t[i]) j ++ ;
		ne[i] = j;
	}
	
	int tmp = 0;
	
	for (int i = 0, j = -1; i < n; i ++ ) {
		while (j != -1 && s[i] != t[j + 1]) j = ne[j];
		if (s[i] == t[j + 1]) j ++ ;
		if (j == m - 1) {
			++ tmp;
			j = -1;
		}
	}
	return tmp >= k;
}

inline void solve(){
	cin>>n>>Yss1206>>k;
	cin >> s ;
	int l = 0 , r = n/k ;
	while(l<r){
		int mid = l + r + 1 >> 1 ;
		if(pd(mid)) l = mid ;
		else r = mid - 1 ; 
	}
	cout << l << endl ;
}
 
signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

G2. Division + LCP (hard version)

后面再补

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

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

相关文章

【Spring】Spring中AOP的简介和基本使用,SpringBoot使用AOP

&#x1f4dd;个人主页&#xff1a;哈__ 期待您的关注 目录 一、AOP简介 二、AOP个人浅谈 三、AOP中几个核心的方法注解 四、AOP中几个核心的属性 1.切入点&#xff08;PointCut&#xff09; 五、代码演示 1.SpringBoot引入依赖 2.定义一个AOP&#xff0c;也就是切面…

【数据库主从架构】

【数据库主从架构】 1. 什么是数据库的主从架构1.1 主从复制1.1.1 MySQL的主从主从复制技术三级目录 1. 什么是数据库的主从架构 随着公司业务线的增多&#xff0c;各种数据都在迅速增加&#xff0c;并且数据的读取流量也大大增加&#xff0c;就面临着数据安全问题&#xff0c;…

ICode国际青少年编程竞赛- Python-1级训练场-基础训练1

ICode国际青少年编程竞赛- Python-1级训练场-基础训练1 1、 Dev.step(4)2、 Dev.step(-4) Dev.step(8)3、 Dev.turnLeft() Dev.step(4)4、 Dev.step(3) Dev.turnLeft() Dev.step(-1) Dev.step(4)5、 Dev.step(-1) Dev.step(3) Dev.step(-2) Dev.turnLeft() Dev.step(…

Servlet(一些实战小示例)

文章目录 一、实操注意点1.1 代码修改重启问题1.2 Smart Tomcat的日志1.3 如何处理错误 一. 抓自己的包二、构造一个重定向的响应&#xff0c;让页面重定向到百度主页三、让服务器返回一个html数据四、表白墙4.1 约定前后端数据4.2 前端代码4.3 后端代码4.4 保存在数据库的版本…

超强动画制作软件blender

blender中文手册&#xff1a;Blender 4.1 Manual Blender 是一款集3D建模、渲染、动画、视频编辑、音频处理、游戏设计等多功能于一体的软件。由于其开源性质&#xff0c;它拥有庞大的用户群体和活跃的开发者社区&#xff0c;这使得Blender的功能和性能得到了不断的提升和优化…

Linux CentOS7部署ASP.NET Core应用程序,并配置Nginx反向代理服务器和Supervisor守护服务

前言&#xff1a; 本篇文章主要讲解的是如何在Linux CentOS7操作系统搭建.NET Core运行环境并发布ASP.NET Core应用程序&#xff0c;以及配置Nginx反向代理服务器。因为公司的项目一直都是托管在Window服务器IIS上&#xff0c;对于Linux服务器上托管.NET Core项目十分好奇。因为…

hot100 -- 二叉树(上)

目录 &#x1f382;二叉树的中序遍历 AC 递归 AC 迭代 &#x1f33c;二叉树的最大深度 AC DFS递归 AC BFS &#x1f6a9;翻转二叉树 AC 后序&#xff08;递归&#xff09; AC 中序 &#x1f6a9;对称二叉树 AC 递归 AC 迭代 &#x1f33c;二叉树的直径 A…

C语言之位操作符:<<、>>、、|、^、~,以及原码反码补码和例题详解

目录 前言 一、原码、反码、补码 二、移位操作符 三、位操作符&#xff1a;&、|、^、~ 四、经典例题分析&#xff1a; 总结 前言 本文将详细介绍C语言中左移操作符<<&#xff0c;右移操作符>>&#xff0c;按位与&&#xff0c;按位或|&#xff0c;按位异或^…

VS(Visual Studio)中查找项目里的中文字符

目录 正则表达式查找中文字符 正则表达式查找中文字符 在Visual Studio (VS) 中查找所有的中文字符&#xff0c;你可以使用其强大的查找和替换功能。不过&#xff0c;由于中文字符的范围非常广泛&#xff08;包括简体中文、繁体中文、日本汉字、韩国汉字等&#xff09;&#xf…

C语言——小知识和小细节17

一、未能给指针成功赋值 #include <stdio.h> #include <stdlib.h> #include <string.h>void GetMemory(char* p) {p (char*)malloc(20 * sizeof(char)); }void Test() {char* str NULL;GetMemory(str);strcpy(str, "Hello World!");printf(&quo…

计算机网络chapter2——应用层

文章目录 第2章 应用层章节引出—— 2.1应用层协议原理2.1.1 网络应用程序体系结构&#xff08;1&#xff09;客户-服务器体系结构&#xff08;2&#xff09;对等(P2P)体系结构2.1.2 进程通信1.客户和服务器进程2.进程与计算机网络之间的接口3. 进程寻址 2.1.3 可供应用程序使用…

Linux shell编程学习笔记48:touch命令

0 前言 touch是csdn技能树Linux基础练习题中最常见的一条命令&#xff0c;这次我们就来研究它的功能和用法。 1. touch命令的功能、格式和选项说明 我们可以使用命令 touch --help 来查看touch命令的帮助信息。 purpleEndurer bash ~ $ touch --help Usage: touch [OPTION]…

双指针(C++)

文章目录 1、移动零2、复写零3、快乐数4、盛最多水的容器5、有效三角形的个数6、和为s的两个数7、三数之和8、四数之和 需要理解的是&#xff0c;双指针并非只有指针&#xff0c;双指针的意思是两个位置。比如对于数组来说&#xff0c;两个下标也是双指针。当然&#xff0c;也可…

基础IO认识

回顾文件 我们之前认识文件只是在语言程度上理解&#xff0c;但是我们理解的不够彻底&#xff0c;要想真正理解文件要在os上理解。 简单代码认识 1 #include<stdio.h>2 int main(){3 FILE* fpfopen("log.txt","w");4 if(fpNULL){5 p…

【小浩算法 BST与其验证】

BST与其验证 前言我的思路思路一 中序遍历判断数组无重复递增思路二 递归边界最大值最小值的传递 我的代码测试用例1测试用例2 前言 BST是二叉树一个经典应用&#xff0c;我们常常将其用于数据的查找以及构建平衡二叉树等。今天我所做的题目是验证一颗二叉树是否为二叉搜索树&…

Web,Sip,Rtsp,Rtmp,WebRtc,专业MCU融屏视频混流会议直播方案分析

随着万物互联&#xff0c;视频会议直播互动深入业务各方面&#xff0c;主流SFU并不适合管理&#xff0c;很多业务需要各种监控终端&#xff0c;互动SIP硬件设备&#xff0c;Web在线业务平台能相互融合&#xff0c;互联互通&#xff0c; 视频混流直播&#xff0c;录存直播推广&a…

带环链表问题

带环链表就是字面意思带环的链表&#xff0c;例如以下这三种情况 练习题 1.给定一个链表&#xff0c;判断链表中是否带环. - 力扣&#xff08;LeetCode&#xff09; 思路&#xff1a;快慢指针&#xff0c;慢指针走一步&#xff0c;快指针走两步&#xff0c;两个指针从链表的起…

硅片和SOI哪个研究方向更好?

知识星球&#xff08;星球名&#xff1a;芯片制造与封测社区&#xff0c;星球号&#xff1a;63559049&#xff09;里的学员问&#xff1a;我研一将要结束&#xff0c;即将进入课题组。我们课题组方向有硅片和soi两种方向&#xff0c;这两种方向该如何选择呢&#xff1f; 硅片与…

python离线安装包的方法

python离线安装包的方法 访问对应安装包的镜像文件的网站找到适合自己的whl文件安装 访问对应安装包的镜像文件的网站 https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/<包名>/找到适合自己的whl文件 安装 下载完成后&#xff0c;进入opencv_python-3.4.11.45-c…

这是一个简单网站,后续还会更新

1、首页效果图 代码 <!DOCTYPE html> <html> <head> <meta charset"utf-8" /> <title>爱德照明网站首页</title> <style> /*外部样式*/ charset "utf-8"…