-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8983.cpp
More file actions
84 lines (80 loc) · 1.47 KB
/
8983.cpp
File metadata and controls
84 lines (80 loc) · 1.47 KB
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
81
82
83
84
// 8983. 사냥꾼
// 2019.05.21
// 이진 탐색
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
pair<int, int> animal[100001];
int gun[100001]; // 사대의 위치
int m, n, l;
// pair의 first로 오름차순 정렬하는 compare
bool compare(pair<int, int>& a, pair<int, int>&b)
{
return a.first < b.first;
}
// 주어진 동물의 x좌표에서 가장 가까운 사대의 위치 index 반환
// 이진탐색으로 찾음
int FindNearest(int left, int right, int target)
{
int mid;
if (right == 0)
{
return left;
}
if (left > right)
{
if (abs(target - gun[left]) > abs(target - gun[right]))
{
return right;
}
else
{
return left;
}
}
mid = (left + right) / 2;
if (gun[mid] < target)
{
return FindNearest(mid + 1, right, target);
}
else if (gun[mid] > target)
{
return FindNearest(left, mid - 1, target);
}
else
{
return mid;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> m >> n >> l;
for (int i = 1; i <= m; i++)
{
cin >> gun[i];
}
for (int i = 1; i <= n; i++)
{
int x, y;
cin >> x >> y;
animal[i] = { x,y };
}
// 동물의 x좌표, 사대의 위치 오름차순 정렬
sort(animal + 1, animal + n + 1, compare);
sort(gun + 1, gun + m + 1);
int ans = 0;
for (int i = 1; i <= n; i++)
{
int tmp = FindNearest(1, m, animal[i].first);
// 동물을 잡을 수 있다면
if (abs(gun[tmp] - animal[i].first) + animal[i].second <= l)
{
ans++;
}
}
cout << ans << endl;
return 0;
}