-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathF.cpp
More file actions
106 lines (100 loc) · 1.6 KB
/
F.cpp
File metadata and controls
106 lines (100 loc) · 1.6 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
void solve(vector<int> arr, int k)
{
vector<int>::iterator last, first;
int i = 0;
int l, r, l_max, r_max;
int occ;
int m = -1;
sort(arr.begin(), arr.end());
while (i < arr.size())
{
first = lower_bound(arr.begin(), arr.end(), arr[i]);
last = upper_bound(arr.begin(), arr.end(), arr[i]);
occ = last - first;
if (occ >= k)
{
m = 0;
l = arr[i];
l_max = arr[i];
r = arr[i];
r_max = arr[i];
i += occ;
break ;
}
i += occ;
}
if (m == -1)
{
cout << -1 << endl;
return ;
}
i = 0;
while (i < arr.size())
{
if (arr[i] - r > 1)
{
if (r - l > m)
{
l_max = l;
r_max = r;
m = r - l;
}
l = arr[i];
r = arr[i];
continue ;
}
first = lower_bound(arr.begin(), arr.end(), arr[i]);
last = upper_bound(arr.begin(), arr.end(), arr[i]);
occ = last - first;
if (last - first < k)
{
if (r - l > m)
{
l_max = l;
r_max = r;
m = r - l;
}
l = arr[i + occ];
r = arr[i + occ];
}
else
r = arr[i];
i += occ;
}
if (m != -1)
{
//cout << r << "|" << l << endl;
if (r - l > m)
{
l_max = l;
r_max = r;
}
cout << l_max << " " << r_max << endl;
}
else
cout << -1 << endl;
}
int main()
{
int cases;
int size, k, val;
vector<int> arr;
cin >> cases;
while (cases--)
{
cin >> size >> k;
while (size--)
{
cin >> val;
arr.push_back(val);
}
solve(arr, k);
arr.clear();
}
return (0);
}