-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1419d2.cpp
More file actions
101 lines (70 loc) · 1.72 KB
/
1419d2.cpp
File metadata and controls
101 lines (70 loc) · 1.72 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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// Idea: Binary search to find the max ice spheres that can be bought.
// If x of the cheapest spheres need to be bought, x + 1 of the
// most expensive spheres must "pad" them. Check this.
void solve() {
int n;
cin >> n;
vector<int> a(n + 2);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a.begin() + 1, a.begin() + n + 1);
auto check = [&] (int val) -> bool { // Can we take the first 'val' ice spheres?
if (val >= (n - val)) {
return false;
}
int l = n - val;
int cnt = 0;
for (int i = 1; i <= val; i++) {
cnt += (a[i] < a[l] && a[i] < a[++l]);
}
return cnt == val;
};
int l = 0, r = n / 2 - !(n & 1);
while (l < r) {
int mid = (l + r) / 2;
if (check(mid)) {
l = mid + 1;
} else {
r = mid;
}
}
l -= !check(l);
cout << l << '\n';
int rem = n - l;
vector<int> cur(n + 2);
cur[1] = a[rem++];
int ind = 2;
for (int i = 1; i <= l; i++) {
cur[ind++] = a[i];
cur[ind++] = a[rem++];
}
int cnt = 0;
for (int i = 2; i < n; i++) {
if (cur[i] != 0 && cur[i + 1] != 0) {
cnt += (cur[i] < cur[i - 1] && cur[i] < cur[i + 1]);
}
}
for (int i = l + 1; i < rem; i++) {
if (ind > n) {
break;
}
cur[ind++] = a[i];
}
for (int i = 1; i <= n; i++) {
cout << cur[i] << ' ';
}
cout << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}