Training/HackerRank

[C++][STL] Lower Bound-STL

FATKITTY 2021. 8. 8. 01:07
반응형

https://www.hackerrank.com/challenges/cpp-lower-bound/problem

 

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int n, q, y, temp;
    cin >> n;
    vector<int> arr;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        arr.push_back(temp);
    }
    //sort(arr.begin(), arr.end());

    cin >> q;
    vector<int>::iterator low;
    vector<int>::iterator iter;
    iter = arr.begin();
    for (int i = 0; i < q; i++) {
        cin >> y;
        low = lower_bound(arr.begin(), arr.end(), y);
        if (y == arr[low - iter]) cout << "Yes " << (low - iter + 1) << endl;
        else cout << "No " << (low - iter + 1) << endl;
    }
    
    return 0;
}

 

Reference

http://www.cplusplus.com/reference/algorithm/lower_bound/

 

반응형

'Training > HackerRank' 카테고리의 다른 글

[C++][STL] Maps-STL  (0) 2021.11.21
[C++][STL] Sets-STL  (0) 2021.08.17
[C++][Introduction] Variable Sized Arrays  (0) 2021.08.07
[C++][STL] Vector-Erase  (0) 2021.08.07
[C++][Classes] Class  (0) 2021.08.07