-
Notifications
You must be signed in to change notification settings - Fork 3
/
lightoj 1084.cpp
58 lines (49 loc) · 876 Bytes
/
lightoj 1084.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
// Created by ash_98
#include<bits/stdc++.h>
using namespace std;
#define mx 200005
#define ll long long
#define mod 1000000007 //998244353
int ar[mx];
int dp[mx];
int vis[mx];
int n,m,ii,k;
int func(int i)
{
if(i>n)return 0;
if(vis[i]==ii)return dp[i];
vis[i]=ii;
int re=mx;
int j=lower_bound(ar+i+1,ar+n+1,ar[i]+k+k)-ar;
if(j==n+1 or ar[j]>(ar[i]+k+k))j--;
if(j-i>=2)
{
re=min(re,1+func(j+1));
}
if(j-i>=3)
{
re=min(re,1+func(j));
}
if(j-i>=4)
{
re=min(re,1+func(j-1));
}
return dp[i]=re;
}
void solve()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)scanf("%d",&ar[i]);
sort(ar+1,ar+n+1);
ii++;
int re=func(1);
if(re>n)re=-1;
printf("Case %d: %d\n",ii,re );
}
int main()
{
int t=1;
scanf("%d",&t);
while(t--)solve();
return 0;
}