forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1.cpp
More file actions
37 lines (30 loc) Β· 1013 Bytes
/
1.cpp
File metadata and controls
37 lines (30 loc) Β· 1013 Bytes
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
#include <bits/stdc++.h>
using namespace std;
// κ°μ΄ [left_value, right_value]μΈ λ°μ΄ν°μ κ°μλ₯Ό λ°ννλ ν¨μ
int countByRange(vector<int>& v, int leftValue, int rightValue) {
vector<int>::iterator rightIndex = upper_bound(v.begin(), v.end(), rightValue);
vector<int>::iterator leftIndex = lower_bound(v.begin(), v.end(), leftValue);
return rightIndex - leftIndex;
}
int n, x;
vector<int> v;
int main() {
// λ°μ΄ν°μ κ°μ N, μ°Ύκ³ μ νλ κ° x μ
λ ₯λ°κΈ°
cin >> n >> x;
// μ 체 λ°μ΄ν° μ
λ ₯ λ°κΈ°
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
v.push_back(temp);
}
// κ°μ΄ [x, x] λ²μμ μλ λ°μ΄ν°μ κ°μ κ³μ°
int cnt = countByRange(v, x, x);
// κ°μ΄ xμΈ μμκ° μ‘΄μ¬νμ§ μλλ€λ©΄
if (cnt == 0) {
cout << -1 << '\n';
}
// κ°μ΄ xμΈ μμκ° μ‘΄μ¬νλ€λ©΄
else {
cout << cnt << '\n';
}
}