跳到正文

个人记录

11.19日训练课总结

LICHENYU12345约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 5 分钟

版本

  1. v2当前
  2. v1

B4362 eqal_range

题意:

  • 给你1个长度为n的数组a有q次询问
  • 询问1个数字x问你数组a里最小的比x大的数是多少?
  • a里最大的比x小的数是多少?

思路:

  • 这个题目我们可以使用二分查找来解决这道题
  • 根据题意我们需要优化输出输出所以我们可以用这样1个代码来优化输入输出
C++
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

  • 排序后可以首先根据题意使用函数upper_bound()也就是查找大于x的最小值的下标
  • pos变量=下标
  • 判断如果pos大于n+1就等于没找到就输出-1,否则输出a数组的第pos个值
  • 细节输出空格

  • 和上1个操作一样但现在他让我们输出的是小于x的最后1个数字是多上但我们的lower_bound()函数是查找第1个大于等于x的数字所以要-1才能得到最终下标
  • pos=下标
  • 同样判断如果pos如果小于1就是没有找到所以输出-1,否则输出a数组第pos个下标
  • 细节换行‘\n’

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5;
int n,m,a[N],x,pos;
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	while(m--)
	{
		cin>>x;
		int pos=upper_bound(a+1,a+n+1,x)-a;
		if(pos==n+1)
		{
			cout<<-1;
		}
		else
		{
			cout<<a[pos];
		}
		cout<<' ';
		pos=lower_bound(a+1,a+n+1,x)-a-1;
		if(pos<1)
		{
			cout<<-1;
		}
		else
		{
			cout<<a[pos];
		}
		cout<<'\n';
	}

	return 0;
}

P1830 轰炸III

题意:

  • 有1个面积为n*m的城市遭受了x次
  • x行,每行4个整数x1,x2,y1,y2 ,表示被轰炸的矩形的左上角坐标和右下角坐标
  • 和y行关键坐标,每行2个整数c,d
  • 输出关键坐标是否被炸,被炸输出Y并且输出被炸了几次和最后1次是第几轮

思路:

  • 首先城市是1个n*m的矩形所以定义1个二维数组a
  • 输入完轰炸的左上角坐标和右下角坐标后在写2个for循环遍历整个轰炸区域在用1个桶数组记,在用1个二维数组变量las录计每1个位置被轰炸了多少次
  • 再用1个变量记录最后1次被轰炸

上述就是最简单的方法


  • 输入关键坐标后判断a[c][d]如果等于0就是表示没有被炸输出NN,否则就输出YY 在输出a[c][d]t桶数组,最后输出las[c][d]后1次被轰炸的轮数

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[105][105],las[105][105],n,m,x,y,tx,ty,nx,ny,c,d;
signed main()
{

	cin>>n>>m>>x>>y;
	int t  = 1;
	while(x--)
	{
		cin>>tx>>ty>>nx>>ny;
		for(int i=tx;i<=nx;i++)
		{
			for(int j=ty;j<=ny;j++)
			{
				a[i][j]++;
				las[i][j]=t;
			}
		}
		t ++;
	}
	while(y--)
	{
		cin>>c>>d;
		if(a[c][d]==0)
		{
			cout<<'N'<<'\n';
		}
		else
		{
			cout<<'Y'<<' '<<a[c][d]<<' '<<las[c][d]<<'\n';
		}
	}
	return 0;
}
//ios::snyc_with_stdio(false),cin.tie(0),cout.tie(0);

P12344 [蓝桥杯 2025 省 B/Python B 第二场] 破解信息

题意:

  • 输入1个字符串s输出字典序最大的回文子序列

思路:

  • 我们想要子序列字典序最大,那么应该使得第1个字符最大
  • 因为这个是需要回文子序列,字符串末尾添加的字符必须和前面的字符相等,所以只会选择最大的字符

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
string s;
signed main()
{
	cin>>s;
	char maxn=0;
	for(int i=0;i<s.size();i++)
	{
		maxn=max(maxn,s[i]);
	}
	for(int i=0;i<s.size();i++)
	{
		if(maxn==s[i])
		{
			cout<<s[i];
		}
	}
	return 0;
}
原文