-
Notifications
You must be signed in to change notification settings - Fork 0
/
개구리뛰기.cpp
60 lines (51 loc) · 994 Bytes
/
개구리뛰기.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
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define min(n, m) n > m ? m : n
int a[1000002];
int d[1000002];
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tc;
cin >> tc;
int inf = 1e8;
for (int test_case = 1; test_case <= tc; test_case++) {
fill(d, d + 1000002, inf);
memset(a, 0, sizeof(a));
int n, k;
cin >> n;
for (int i = 1; i <= n; i++) {
int x; cin >> x;
a[i] = x;
}
cin >> k;
d[0] = 0;
for (int i = 1; i <= n; i++) {
// lgN
int left, right, mid;
left = 0, right = i - 1;
mid = (left + right) / 2;
int idx = -1;
while (left <= right) {
if (a[i] - a[mid] <= k) {
idx = mid;
right = mid - 1;
}
else {
left = mid + 1;
}
mid = (left + right) / 2;
}
if (idx == -1) {
break;
}
d[i] = d[idx] + 1;
}
int ans = (d[n] == inf) ? -1 : d[n];
cout << "Case #" << test_case << '\n';
cout << ans << '\n';
}
return 0;
}