C组暑假第一次训练题解

A.寄包柜

题意

两种操作

1.在第i个柜子第j个格子输入

2.输出第i个柜子第j个格子的数字

分析

因为i和j最大为1e5,使用二维数组会爆空间,使用map即可解决

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<iomanip>
#include<tuple>
#include<numeric>
//#include<stack>
//#include<deque>
//#include<bitset>
//#include<functional>

#define ll long long
#define inf 0x3f3f3f3f
#define lc p<<1
#define rc p<<1|1
#define endl '\n'
#define all(a) a.begin()+1,a.end()
#define pb push_back
using namespace std;

typedef pair<int,int>PII;

void solve()
{
	int n,q;
	cin>>n>>q;
	map<PII,int>mp;//这里的PII为pair<int,int>二元组
	while(q--)
	{
		int op;cin>>op;
		if(op==1)
		{
			int x,y,k;
			cin>>x>>y>>k;
			mp[{x,y}]=k;
		}
		else
		{
			int x,y;
			cin>>x>>y;
			cout<<mp[{x,y}]<<endl;
		}
	}
	
	
}
	
	
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	
	solve();
	
	return 0;
}

B.不重复数字

题意

给定n个数只保留第一次出现的数

分析

使用map或set都可以,但本题卡输入输出,需要关闭同步流

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<iomanip>
#include<tuple>
#include<numeric>
//#include<stack>
//#include<deque>
//#include<bitset>
//#include<functional>

#define ll long long
#define inf 0x3f3f3f3f
#define lc p<<1
#define rc p<<1|1
#define endl '\n'
#define all(a) a.begin()+1,a.end()
#define pb push_back
using namespace std;

typedef pair<int,int>PII;

void solve()
{
	int n;cin>>n;
	map<int,int>mp;
	while(n--)
	{
		int x;cin>>x;
		if(!mp[x]) cout<<x<<" ";
		mp[x]++;
	}
	
	cout<<endl;
	
	
}
	
	
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--)
	solve();
	
	return 0;
}

C.文字处理软件

题意

4种操作,涉及到substr()函数和find()函数,具体用法自行搜索

代码

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
string s,news;
int n,k;
int main()
{
    cin>>n>>news;
    while(n--)
    {
        cin>>k;
        if(k==1)
        {
            string ss;
            cin>>ss;
            news=news+ss;
            cout<<news<<endl;
        }
        
       else if(k==2)
        {
            int a,b;
            cin>>a>>b;
            news=news.substr(a,b);
            cout<<news<<endl;
        }
      else   if(k==3)
        {
            int a;
            string ss;
            cin>>a>>ss;
            news=news.insert(a,ss);
            cout<<news<<endl;
        }
       else if(k==4)
        {
            string ss;
            cin>>ss;
            if(news.find(ss)<news.size()) cout<<news.find(ss)<<endl;
            else cout<<-1<<endl;
        }
    }
    
    
    return 0;
}

 

D.ICPC Balloons

题意

给定只含大写字母的字符串,第一次出现的字母发两个气球,第二次出现后只发一个气球,求一共发多少气球

分析

map记录每个字母出现次数即可

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<iomanip>
#include<tuple>
#include<numeric>
//#include<stack>
//#include<deque>
//#include<bitset>
//#include<functional>

#define ll long long
#define inf 0x3f3f3f3f
#define lc p<<1
#define rc p<<1|1
#define endl '\n'
#define all(a) a.begin()+1,a.end()
#define pb push_back
using namespace std;

typedef pair<int,int>PII;

void solve()
{
	int n;cin>>n;
	string s;cin>>s;
	map<char,int>mp;
	int ans=0;
	for(auto c:s)
	{
		if(!mp[c]) ans+=2;
		else ans++;
		mp[c]++;
	}
	cout<<ans<<endl;
	
}
	
	
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--)
	solve();
	
	return 0;
}

E.Remove Prefix

题意

给定n个整数,问删除前几个能使得剩下的数字只出现一次

分析

我们从后往前跑,将每个数字加入到set中直到出现重复数字,或者使用map维护每个数字出现次数

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<iomanip>
#include<tuple>
#include<numeric>
//#include<stack>
//#include<deque>
//#include<bitset>
//#include<functional>

#define ll long long
#define inf 0x3f3f3f3f
#define lc p<<1
#define rc p<<1|1
#define endl '\n'
#define all(a) a.begin()+1,a.end()
#define pb push_back
using namespace std;

typedef pair<int,int>PII;

void solve()
{
	int n;cin>>n;
	vector<int>f(n+1);
	for(int i=1;i<=n;i++) cin>>f[i];
	set<int>s;
	for(int i=n;i>=1;i--)
	{
		if(s.count(f[i])) {cout<<i<<endl;return;}
		else s.insert(f[i]);
	}
	
	cout<<0<<endl;//若前面没有return的话说明数组本身就无重复数字
}
	
	
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--)
	solve();
	
	return 0;
}

F.Meximization

题意 

给定一个数组a,对其重新排序使其所有前缀的mex之和最大,其中mex为不在集合中的最小非负整数,例如  MEX({1,2,3})=0、 MEX({0,1,2,4,5})=3

分析

根据mex的定义我们容易想到,如果我们将一个在原数列中已有的数放进去,那么 mex⁡ 值必定是不变的。所以,我们将所有数不重复地从小到大放入新数列中,再将原来没选进去的数按任意顺序放在最后,可以证明这样放的 mex⁡ 值最大。

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<iomanip>
#include<tuple>
#include<numeric>
//#include<stack>
//#include<deque>
//#include<bitset>
//#include<functional>

#define ll long long
#define inf 0x3f3f3f3f
#define lc p<<1
#define rc p<<1|1
#define endl '\n'
#define all(a) a.begin()+1,a.end()
#define pb push_back
using namespace std;

typedef pair<int,int>PII;

void solve()
{
	int n;cin>>n;
	vector<int>f(n+1);
	map<int,int>mp;
	int mx=-1e9;
	for(int i=1;i<=n;i++)
	{
		cin>>f[i];
		mp[f[i]]++;
		mx=max(mx,f[i]);//记录最大值
	}
	//第一次从小到大输出一遍
	for(int i=0;i<=mx;i++)
	{
		if(mp[i])
		{
			cout<<i<<" ";
			mp[i]--;
		}
	}
	//第二次将剩余的数输出
	for(int i=0;i<=mx;i++)
	{
		while(mp[i])
		{
			cout<<i<<" ";
			mp[i]--;
		}
	}
	cout<<endl;
}
	
	
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--)
	solve();
	
	return 0;
}


 G.Alice and Books

题意

翻译貌似有点小问题,但通过样例可以发现对于第一堆,Alice看的是页数最多的书,对于第二堆,Alice选择的是编号最大的书,也就是除了第n本书其中页数最多的书加上n即为答案

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<iomanip>
#include<tuple>
//#include<stack>
//#include<deque>
//#include<bitset>
//#include<functional>

#define ll long long
#define inf 0x3f3f3f3f
#define lc p<<1
#define rc p<<1|1
#define endl '\n'
#define all(a) a.begin()+1,a.end()
#define pb push_back
using namespace std;

typedef pair<int,int>PII;

void solve()
{
	int n;cin>>n;
	vector<int>f(n+1);
	for(int i=1;i<=n;i++) cin>>f[i];
	
	int k=0;
	for(int i=1;i<n;i++) k=max(f[i],k);
	cout<<k+f[n]<<endl;
	
	
}
	
	
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--)
	solve();
	
	return 0;
}

H.Different String

题意

给定一个字符串,判断重新排列后其是否能形成一个新的字符串

分析

很明显只要字符串不全是一种字母就一定可以

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<iomanip>
//#include<stack>
//#include<deque>
//#include<bitset>
//#include<functional>

#define ll long long
#define inf 0x3f3f3f3f
#define lc p<<1
#define rc p<<1|1
#define endl '\n'
#define all(a) a.begin()+1,a.end()
#define pb push_back
using namespace std;

typedef pair<int,int>PII;

void solve()
{
	string s;cin>>s;
	map<char,int>mp;
	for(auto c:s) mp[c]++;
	if(mp.size()==1) cout<<"NO"<<endl;
	else
	{
		cout<<"YES"<<endl;
		cout<<s.substr(1,s.size()-1)<<s[0]<<endl;
	}
	
	
}
	
	
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--)
	solve();
	
	return 0;
}

I.Matrix Stabilization

题意

给定一个n*m的矩阵,重复以下操作

若存在单元格(i,j)严格小于其所有相邻单元格(四周),则将该单元格值减1,若存在多个则选择i最小的,还存在多个选择j最小的

输出不能执行操作后的单元格

分析

观察样例可以发现,对于一个满足条件可以操作的单元格,其最终的值一定是其周围单元格值的最大值,基于以上性质,我们遍历矩阵,将所有可操作的单元格更改为最终值即可

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<iomanip>
#include<tuple>
//#include<stack>
//#include<deque>
//#include<bitset>
//#include<functional>

#define ll long long
#define inf 0x3f3f3f3f
#define lc p<<1
#define rc p<<1|1
#define endl '\n'
#define all(a) a.begin()+1,a.end()
#define pb push_back
using namespace std;

typedef pair<int,int>PII;
const int N=110;
int f[N][N];
void solve()
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=110;i++)
	{
		for(int j=1;j<=110;j++) f[i][j]=0;
	}
	
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++) cin>>f[i][j];
	}
	
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(f[i][j]>f[i-1][j] && f[i][j]>f[i+1][j] && f[i][j]>f[i][j-1] && f[i][j]>f[i][j+1])
			{
				f[i][j]=max({f[i-1][j],f[i+1][j],f[i][j-1],f[i][j+1]});
			}
			cout<<f[i][j]<<" ";
		}
		cout<<endl;
	}
	
	
}
	
	
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--)
	solve();
	
	return 0;
}

J.Shifts and Sorting

题意

给定一个01字符串,每次可执行以下操作

选择l和r,将s[l]和s[r]互换位置,代价为r-l+1,即所选子串长度

求将字符串变为非降序(即0在前,1在后)的最小代价

分析

贪心,如果当前位置是0,那么我们就将其与从左到右的第一个1互换(如果前面有1的话)

,这样可以保证所有1都在该0之前,我们用cnt记录1的个数,那么遇到0时交换产生的代价为cnt+1

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<iomanip>
//#include<stack>
//#include<deque>
//#include<bitset>
//#include<functional>

#define ll long long
#define inf 0x3f3f3f3f
#define lc p<<1
#define rc p<<1|1
#define endl '\n'
#define all(a) a.begin()+1,a.end()
using namespace std;

typedef pair<int,int>PII;

void solve()
{
	string s;cin>>s;
	ll cnt=0;
	ll ans=0;
	for(int i=0;i<s.size();i++)
	{
		if(s[i]=='1') cnt++;
		else
		{
			if(cnt!=0) ans+=cnt+1;
		}
	}
	cout<<ans<<endl;
	
}
	
	
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--)
	solve();
	
	return 0;
}

 

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

C++ 什么是虚函数?什么是纯虚函数,以及区别?(通俗易懂)

&#x1f4da; 当谈到虚函数时&#xff0c;通常是指在面向对象编程中的一种机制&#xff0c;它允许在派生类中重写基类的函数&#xff0c;并且能够通过基类指针或引用调用派生类中的函数。 目录 前言 &#x1f525; 虚函数 &#x1f525; 纯虚函数 &#x1f525; 两者区别…

用 Echarts 画折线图

https://andi.cn/page/621503.html

leetcode每日一题-3033. 修改矩阵

题目描述&#xff1a; 解题思路&#xff1a;简单题目&#xff0c;思路非常直接。对列进行遍历&#xff0c;记录下最大值&#xff0c;然后再遍历一遍&#xff0c;把-1替换为最大值。需要注意的是进行列遍历和行遍历是不同的。 官方题解&#xff1a; class Solution { public:v…

VRay渲染有什么技巧?渲染100邀请码1a12

渲染是视觉行业非常重要的一环&#xff0c;没有渲染就没有效果图&#xff0c;常用的渲染器有Vray&#xff0c;而Vray渲染有很多技巧&#xff0c;可以让渲染更快更省&#xff0c;下面我们总结下。 1、删除无用对象 检查场景&#xff0c;看是否有一些不需要渲染的物体和灯光&am…

将大型语言模型模块化打造协作智能体

B UILDING C OOPERATIVE E MBODIED A GENTS MODULARLY WITH L ARGE L ANGUAGE M ODELS 论文链接&#xff1a; https://arxiv.org/abs/2307.02485https://arxiv.org/abs/2307.02485 1.概述 在去中心化控制及多任务环境中&#xff0c;多智能体合作问题因原始感官观察、高昂…

绝区肆--2024 年AI安全状况

前言 随着人工智能系统变得越来越强大和普及&#xff0c;与之相关的安全问题也越来越多。让我们来看看 2024 年人工智能安全的现状——评估威胁、分析漏洞、审查有前景的防御策略&#xff0c;并推测这一关键领域的未来可能如何。 主要的人工智能安全威胁 人工智能系统和应用程…

el-date-picker 设置默认值为当前日期

this.listQuery.Date new Date().toISOString().substr(0, 10); <el-date-picker v-model"listQuery.Date" format"yyyy-MM-dd" value-format"yyyy-MM-dd" type"date" placeholder"选择日期" change"getList()&qu…

Java语言程序设计篇一

Java语言概述 Java语言起源编程语言最新排名名字起源Java语言发展历程Java语言的特点Java虚拟机垃圾回收Java语言规范Java技术简介Java程序的结构Java程序注意事项&#xff1a;注释编程风格练习 Java语言起源 1990年Sun公司提出一项绿色计划。1992年语言开发成功最初取名为Oak…

Blender新手入门笔记收容所(一)

基础篇 基础操作 视角的控制 控制观察视角&#xff1a;鼠标中键平移视图&#xff1a;Shift鼠标中键缩放视图&#xff1a;滚动鼠标中键滚轮 选中物体后&#xff1a;移动物体快捷键G&#xff0c;移动后单击鼠标就会定下来。 进入移动状态后&#xff1a;按Y会沿着Y轴移动进入移动…

成人高考本科何时报名-深职训学校帮您规划学习之路

你有想过继续深造自己的学历吗&#xff1f;也许你已经工作多年&#xff0c;但总觉得学历是一块心病&#xff0c;想要通过成人高考本科来提升自己。不用着急&#xff0c;今天我们来聊一聊成人高考本科的报名时间&#xff0c;以及深职训学校如何帮助你顺利完成报名。 深圳成人高…

2024上半年网络工程师考试《应用技术》试题一

阅读以下说明&#xff0c;回答问题。 【说明】 MPLS基于(1)进行转发&#xff0c;进行MPLS标签交换和报文转发的网络设备称为(2)&#xff0c;构成MPLS域(MPSDomain)。位于MPLS域边缘、连接其他网络的LSR称为(3),区域内部的LSR称为核心LSR(CoreLSR)IP报文进入MPLS网络时&#xf…

文件管理下:文件函数的学习

前言 Hello,小伙伴们你们的作者君又来了&#xff0c;上次我们简单介绍了文件的坐拥并简单提到了数据的读取&#xff0c;和C语言的默认流的作用&#xff0c;今天我将继续带领大家探索文件的奥秘&#xff0c;大家准别好了吗&#xff1f; 在内容开始之前还是按照惯例&#xff0c…

Alt与Tab切换窗口时将Edge多个标签页作为一个整体参与切换的方法

本文介绍在Windows电脑中&#xff0c;使用Alt与Tab切换窗口时&#xff0c;将Edge浏览器作为一个整体参与切换&#xff0c;而不是其中若干个页面参与切换的方法。 最近&#xff0c;需要将主要使用的浏览器由原本的Chrome换为Edge&#xff1b;但是&#xff0c;在更换后发现&#…

Python爬虫系列-让爬虫自己写爬虫(半自动化,代替人工写爬虫)

现在的PC、手机客户端等终端设备大量使用了网页前后端技术&#xff0c;另外主流的网站也会经常会更新&#xff0c;导致以前一个月更新一次爬虫代码&#xff0c;变成了天天需要更新代码&#xff0c;所以自动化爬虫技术在当前就显得特别重要&#xff0c;最近我也是在多次更新某个…

Java | Leetcode Java题解之第220题存在重复元素III

题目&#xff1a; 题解&#xff1a; class Solution {public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {int n nums.length;Map<Long, Long> map new HashMap<Long, Long>();long w (long) t 1;for (int i 0; i < n; i) {long i…

【Java探索之旅】初识多态_概念_实现条件

文章目录 &#x1f4d1;前言一、多态1.1 概念1.2 多态的实现条件 &#x1f324;️全篇总结 &#x1f4d1;前言 多态作为面向对象编程中的重要概念&#xff0c;为我们提供了一种灵活而强大的编程方式。通过多态&#xff0c;同一种操作可以应用于不同的对象&#xff0c;并根据对象…

【Python迭代器探秘】:揭秘迭代器与生成器的魔法,掌握高效循环的艺术

文章目录 一、迭代器的基本概念1.1 迭代器优点1.2 迭代器的编写方法1.3 python内置迭代器函数1.4 小结1.5 迭代器对象与迭代对象1.5.1 区别1. 迭代对象2. 迭代器对象3. 小结 1.5.2 方法区分 二、生成器基本概念1. 生成器函数2. 生成器表达式 一、迭代器的基本概念 迭代器是Pyt…

一.2.(2)基本共射放大电路组成、工作原理;

1.基本共射放大电路组成 共什么取决于输入输出&#xff0c;共剩下的那一极 2.工作原理 输入信号ui通过电容C1加到三极管的基 极&#xff0c;引起基极电流iB的变化&#xff0c;iB的变化又使集电极电流ic发生变 化&#xff0c;且ic的变化量是iB变化量的β倍。由于有集电极电压&…

【数据结构】05.双向链表

一、双向链表的结构 注意&#xff1a;这里的“带头”跟前面我们说的“头节点”是两个概念&#xff0c;带头链表里的头节点&#xff0c;实际为“哨兵位”&#xff0c;哨兵位节点不存储任何有效元素&#xff0c;只是站在这里“放哨的”。 “哨兵位”存在的意义&#xff1a;遍历循…

Django QuerySet对象,filter()方法

filter()方法 用于实现数据过滤功能&#xff0c;相当于sql语句中的where子句。 filter(字段名__exact10) 或 filter(字段名10)类似sql 中的 10 filter(字段名__gt10) 类似SQL中的 >10 filter(price__lt29.99) 类似sql中的 <29.99 filter(字段名__gte10, 字段名__lte20…