-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lower Bound-STL.cpp
80 lines (65 loc) · 1.76 KB
/
Lower Bound-STL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n,q,val,e;
vector<int> v;
vector<int>::iterator low;
cin>>n;
for(auto i=0;i<n;i++)
{
cin>>e;
v.emplace_back(e);
}
cin>>q;
for (int i=0; i<q; i++)
{
cin >> val;
low = lower_bound(v.begin(), v.end(), val);
if (v[low - v.begin()] == val)
cout << "Yes " << (low - v.begin()+1) << endl;
else
cout << "No " << (low - v.begin()+1) << endl;
}
return 0;
}
/*
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
*/
/*
// lower_bound/upper_bound Example
#include <iostream> // std::cout
#include <algorithm> // std::lower_bound, std::upper_bound, std::sort
#include <vector> // std::vector
int main () {
int myints[] = {10,20,30,30,20,10,10,20};
std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
std::vector<int>::iterator low,up;
low=std::lower_bound (v.begin(), v.end(), 20); // ^
up= std::upper_bound (v.begin(), v.end(), 20); // ^
std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
std::cout << "upper_bound at position " << (up - v.begin()) << '\n';
return 0;
}
*/