#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
int n;
vector<int>v;
vector<int>::iterator low,up;//与下面的low,up是对应的,即他们是一套的
cin>>n;
for(int i=1;i<=n;i++)
{
int t;
cin>>t;
v.push_back(t);
}
int q;
cin>>q;
while(q--)
{
int y;
cin>>y;
low=lower_bound(v.begin(),v.end(),y);//lower_bound:
功能:查找非递减序列[first,last) 内第一个大于或等于某个元素的位置。
up=upper_bound(v.begin(),v.end(),y);//upper_bound:
功能:查找非递减序列[first,last) 内第一个大于某个元素的位置
if(up-low==0)//即两次查找到的为同一个数,则说明在数组中找不到要查找的数,
cout<<"No "<<(low-v.begin())+1<<endl;//因为数组实际上是从0开始的,所以要加上1,变成下标从1开始。
else
cout<<"Yes "<<(low-v.begin())+1<<endl;
}
return 0;
}
例如:
在数组组中的下标
0 1 2 3 4
1 2 2 2 4
我们要查找 2 我们用upper_bound找到数字在数组中的下标为 1 ,我们又通过low_bound找到数字在数组中的下标为 4,得到的数组下标相减结果为3,所给数中刚好存在3个2。