-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path1283D.cpp
More file actions
executable file
·51 lines (44 loc) · 831 Bytes
/
1283D.cpp
File metadata and controls
executable file
·51 lines (44 loc) · 831 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Problem Code: 1283D
#include <iostream>
#include <vector>
#include <map>
#include <queue>
using namespace std;
void christmas_trees(int n, int m, vector<int>& x) {
long long min_d = 0;
queue<int> q;
vector<int> y;
map<int, int> d;
for (int i = 0; i < n; i++) {
d[x[i]] = 0;
q.push(x[i]);
}
while (y.size() < m && !q.empty()) {
int pos = q.front();
q.pop();
if (d[pos]) {
min_d += d[pos];
y.push_back(pos);
}
if (!d.count(pos - 1)) {
d[pos - 1] = d[pos] + 1;
q.push(pos - 1);
}
if (!d.count(pos + 1)) {
d[pos + 1] = d[pos] + 1;
q.push(pos + 1);
}
}
cout << min_d << endl;
for (int i = 0; i < m; i++)
cout << y[i] << " ";
}
int main() {
int n, m;
cin >> n >> m;
vector<int> x(n);
for (int i = 0; i < n; i++)
cin >> x[i];
christmas_trees(n, m, x);
return 0;
}