P2249 【深基13.例1】查找

https://www.luogu.com.cn/problem/P2249

输入 n(n\le10^6)n(n≤106) 个不超过 10^9109 的单调不减的(就是后面的数字不小于前面的数字)非负整数 a_1,a_2,\dots,a_{n}a1​,a2​,…,an​,然后进行 m(m\le10^5)m(m≤105) 次询问。对于每次询问,给出一个整数 q(q\le10^9)q(q≤109),要求输出这个数字在序列中的编号,如果没有找到的话输出 -1 。

输入格式

第一行 2 个整数 n 和 m,表示数字个数和询问次数。

第二行 n 个整数,表示这些待查询的数字。

第三行 m 个整数,表示询问这些数字的编号,从 1 开始编号。

输出格式

m 个整数表示答案。

输入输出样例

输入 #1复制

11 3
1 3 3 3 5 7 9 11 13 15 15
1 3 6

输出 #1复制

1 2 -1 

这题意义在于让我发现了我之前总结的板子边界又有问题。重新更正了。https://blog.csdn.net/zstuyyyyccccbbbb/article/details/107106402

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e6+10;
typedef long long LL;
LL a[maxn];
LL bsearch(LL l,LL r,LL q)
{
	while(l<r)
	{
		LL mid=(l+r)>>1;
		if(a[mid]>=q) r=mid;
		else l=mid+1;
	}
	return l;
} 
int main(void)
{
	LL n,m;cin>>n>>m;
	for(LL i=1;i<=n;i++) cin>>a[i];
	while(m--)
	{
		LL q;cin>>q;
		LL t=bsearch(1,n+1,q);
	//	cout<<"t=="<<t<<endl;
	//	cout<<"a[t]=="<<a[t]<<endl;
		if(t==n+1||a[t]!=q) cout<<"-1"<<' ';
		else cout<<t<<' ';
	}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值