-
Notifications
You must be signed in to change notification settings - Fork 0
/
E.cpp
63 lines (57 loc) · 1.2 KB
/
E.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
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200007;
const int MOD = 1000000007;
int n, dp[MAX];
pair<int, int> a[MAX];
int f(int k) {
// solve for [0..k]
if (k < 3 || dp[k] != MOD) {return dp[k];}
for (int off = 3; off <= 5; off++) {
dp[k] = min(dp[k], f(k - off) + a[k].first - a[k - off + 1].first);
}
return dp[k];
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a + 1, a + n + 1);
dp[0] = 0;
for (int i = 1; i <= n; i++) {
dp[i] = MOD;
}
cout << f(n) << ' ';
int curr = n;
vector<int> v;
while (curr > 0) {
for (int off = 3; off <= 5; off++) {
if (dp[curr] == dp[curr - off] + a[curr].first - a[curr - off + 1].first) {
v.push_back(off);
curr -= off;
break;
}
}
}
cout << v.size() << '\n';
reverse(v.begin(), v.end());
int color[n + 1], pos = 1;
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i]; j++) {
color[a[pos].second] = i + 1;
pos++;
}
}
for (int i = 1; i <= n; i++) {
cout << color[i] << ' ';
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
solve();
}