-
Notifications
You must be signed in to change notification settings - Fork 34
/
1084-Winter.cpp
99 lines (61 loc) · 1.02 KB
/
1084-Winter.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <iostream>
#include <limits.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <string.h>
using namespace std;
long long a[100005];
bool vis[100005];
int n;
long long k;
int bfs()
{
pair <int, int> temp;
temp.first = 0;
temp.second = 0;
queue < pair <int, int> > q;
q.push(temp);
vis[0] = 1;
while(!q.empty()) {
int i = q.front().first;
int count = q.front().second;
q.pop();
int j = i;
int p;
p = 0;
if(j == n) {
return count;
}
while((a[j] - a[i]) <= 2 * k and j < n) {
j++;
p++;
}
int k;
k = 0;
while((p - k) >= 3) {
if(vis[j - k] == 0) {
q.push(make_pair(j - k, count + 1));
vis[j-k] = 1;
}
k++;
}
}
return -1;
}
int main()
{
int t;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
scanf("%d", &n);
scanf("%lld", &k);
memset(vis, 0, sizeof vis);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
sort(a, a + n);
printf("Case %d: %d\n", cs, bfs());
}
}