forked from sameer2800/Lightoj-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1084-winter.cpp
87 lines (48 loc) · 1013 Bytes
/
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
#include <bits/stdc++.h>
#define ll long long
#define MAX 100100
using namespace std;
ll arr[100010];
ll dp[100010];
ll recursedp(ll curr,ll max,ll dist)
{
if( max- curr < 2)
return INT_MAX;
if(arr[max]- arr[curr] <= 2*dist)
return 1;
if(dp[curr] != 0)
return dp[curr];
ll mini = INT_MAX;
for(int j= curr+2; j <= max;j++){
if(arr[j]- arr[curr] <= 2*dist){
mini = min(mini,1+ recursedp(j+1,max,dist));
}
else
break;
}
dp[curr] = mini;
return dp[curr];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/sameer/sameer/sam.sam","r",stdin);
#endif
ios_base::sync_with_stdio(false);
ll i,j,k,n,m,t,cont;
cin >> t;
ll cases = t;
while(t--){
memset(dp,0,sizeof dp);
cin >> n >> k;
for(i =0; i < n;i++)
cin >> arr[i];
sort(arr,arr+n);
ll ans = recursedp(0,n-1,k);
if(ans < INT_MAX)
cout << "Case " << cases - t << ": " << ans << endl;
else
cout << "Case " << cases - t << ": " << "-1" << endl;
}
return 0;
}