-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbeach_bars.cpp
76 lines (58 loc) · 1.6 KB
/
beach_bars.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct ij {
int i;
int j;
};
void solve() {
int n; cin >> n;
vector<int> positions;
for (int i = 0; i < n; ++i) {
int p; cin >> p;
positions.push_back(p);
}
sort(positions.begin(), positions.end());
int i = 0;
int j = 0;
int maxParasols = -1;
int smallestDistance = 200;
vector<int> bestPositions;
while (i < n && j < n) {
int width = positions[j] - positions[i];
int parasols = j - i + 1;
// Compute score and compare
int distance = (width + 1) / 2;
int bestPosition = positions[i] + distance;
if (parasols > maxParasols || (parasols == maxParasols && distance <= smallestDistance)) {
// New best
if (parasols > maxParasols || distance < smallestDistance) {
bestPositions = vector<int>();
smallestDistance = distance;
}
maxParasols = parasols;
if (width % 2 == 1) { // Odd width => best position not unique
bestPositions.push_back(bestPosition - 1);
}
bestPositions.push_back(bestPosition);
}
// Move window
if (positions[j + 1] - positions[i] < 201 && j < n - 1) {
++j;
} else {
++i;
}
}
cout << maxParasols << " " << smallestDistance << endl;
for (int p : bestPositions) {
cout << p << " ";
}
cout << endl;
}
int main() {
int t; cin >> t;
for (int i = 0; i < t; ++i) {
solve();
}
}