-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAMR10G.cpp
More file actions
87 lines (81 loc) · 2.11 KB
/
AMR10G.cpp
File metadata and controls
87 lines (81 loc) · 2.11 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
85
86
#include<iostream>
using namespace std;
long int l[30000],m[30000];
int n1,n2;
void merge(long int a[30000],int p,int q,int r)
{
int i,j,k,x=0;
n1=q-p+1;
n2=r-q;
for(i=0;i<n1;i++)
l[i]=a[p+i];
for(j=0;j<n2;j++)
m[j]=a[q+j+1];
i=0;
j=0;
for(k=p;k<=r;k++)
{
if(x==0)
{
if(l[i]<=m[j])
{
a[k]=l[i];
i++;
if(i==n1)
x=-1;
}
else
{
a[k]=m[j];
j++;
if(j==n2)
x=1;
}
}
else if(x==-1)
{
a[k]=m[j];
j++;
}
else
{
a[k]=l[i];
i++;
}
}
}
void mergesort(long int a[30000],int p,int r)
{
int q;
if(p<r)
{
q=(p+r)/2;
mergesort(a,p,q);
mergesort(a,q+1,r);
merge(a,p,q,r);
}
}
int main()
{
int n,t,i,j,k;
long int a[30000],min;
cin>>t;
for(i=0;i<t;i++)
{
cin>>n>>k;
for(j=0;j<n;j++)
cin>>a[j];
mergesort(a,0,n-1);
min=1000000000;
for(j=0;j<n-k+1;j++)
{
if(a[j+k-1]-a[j]<min)
min=a[j+k-1]-a[j];
}
cout<<min<<"\n";
}
//for(int i=0;i<n;i++)
//{
// cout<<a[i]<<" ";
//}
}