求助
  • 板块灌水区
  • 楼主ZSB00000
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/11/30 20:42
  • 上次更新2023/11/30 22:37:45
查看原帖
求助
1017629
ZSB00000楼主2023/11/30 20:42

题目:\

二分查找

题目背景

模板题,无背景

题目描述

输入一个整数n和n个整数,保证这n个整数已经按照从小到大进行排序。 然后输入一个整数q(q <= 100000)代表q次查询。接下来q行,每行含有一个整数m,代表一次查询。对于每次查询,使用二分查找判断m是否在之前输入的n个整数中出现过。

如果出现,输出一行"Yes",否则输出"No"。

输入格式

第一行:一个整数n(n <= 100000)。 接下来n行,每行一个整数ai(1 <= ai <= 10^9)。 接下来一行,一个整数q。 接下来q行,每行输入一个整数x(1 <= x <= 10^9)。

输出格式

q行字符串,每行为"Yes"或"No"。

样例 #1

样例输入 #1

5
1
3
4
5
7
3
4
5
0

样例输出 #1

Yes
Yes
No

代码:\

#include<bits/stdc++.h>
using namespace std;
#define int long long
int father[100005];
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>father[i];
	}
	sort(father+1,father+1+n);
	int q;
	cin>>q;
	for(int i=1;i<=q;i++)
	{
		int x;
		cin>>x;
		if(*(lower_bound(father+1,father+1+n,x))==x)
		{
			//cout<<(lower_bound(father+1,father+1+n,x)-father)<<" ";
			//puts("Yes");
			cout<<"Yes\n";
		}
		else
		{
			//cout<<*(lower_bound(father+1,father+1+n,x))<<" ";
			//puts("No");
			cout<<"No\n";
		}
	}
	return 0;
}

0pts,求助

2023/11/30 20:42
加载中...