-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraygame1400.cpp
More file actions
36 lines (34 loc) · 900 Bytes
/
arraygame1400.cpp
File metadata and controls
36 lines (34 loc) · 900 Bytes
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
// #include <bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define int long long
signed main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
if (k >= 3) {
cout << 0 << endl;
continue;
}
sort(begin(a), end(a));
int d = a[0];
for (int i = 0; i < n - 1; i++) d = min(d, a[i + 1] - a[i]);
if (k == 1) {
cout << d << endl;
continue;
}
for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) {
int v = a[i] - a[j];
int p = lower_bound(begin(a), end(a), v) - begin(a);
if (p < n) d = min(d, a[p] - v);
if (p > 0) d = min(d, v - a[p - 1]);
}
cout << d << endl;
}
}