hackerrank>Dashboard>C++>STL>Lower Bound-STL

You are given integers in the sorted order. Then you are given queries. In each query you will be given an integer and you have to tell whether that integer is present in the array, if so you have to tell at which index it is present and if it is not present you have to tell the index at which the smallest integer that is just greater than the given number is present.
Lower bound is a function that can be used with a sorted vector. Learn how to use lower bound to solve this problem by Clicking Here.

Input Format

The first line of the input contains the number of integers . The next line contains integers in sorted order. The next line contains , the number of queries. Then lines follow each containing a single integer .
If the same number is present multiple times, you have to print the first index at which it occurs.
The input is such that you always have an answer for each query.

Constraints

,where is element in the array.


Output Format

For each query you have to print "Yes"(without the quotes)if the number is present and at which index(1-based) it is present separated by a space.
If the number is not present you have to print "No"(without the quotes) followed by the index of the next smallest number just greater than that number.
You have to output each query in a new line.

Sample Input

 8
 1 1 2 2 6 9 9 15
 4
 1
 4
 9
 15

Sample Output

 Yes 1
 No 5
 Yes 6
 Yes 8

输出最近的最小的大于所给值的下标

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#define MAX 1e9+8
using namespace std;
int main()
{
    int N;
    while(~scanf("%d",&N))
    {
        vector<int>vec1;
        while(N--)
        {
            int t;
            scanf("%d",&t);
            vec1.push_back(t);
        }
        cin>>N;
        string s[2]={"No","Yes"};
        while(N--)
        {
            int t;
            cin>>t;
            vector<int>::iterator it;
            it=lower_bound(vec1.begin(),vec1.begin()+vec1.size(),t);
            cout<<s[*it==t]<<' '<<it-vec1.begin()+1<<endl;
        }
    }
    return 0;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值