-
Notifications
You must be signed in to change notification settings - Fork 2
/
18513_josueyeon.cpp
64 lines (50 loc) · 1.16 KB
/
18513_josueyeon.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
// BFS
#include <vector>
#include <queue>
#include <unordered_set>
#include <iostream>
using namespace std;
unordered_set<long long> visited;
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int N, K;
cin>>N>>K;
visited.clear();
priority_queue<pair<long long, long long>> q;
for(int i = 0;i < N;i++)
{
long long temp;
cin>>temp;
q.push({0, temp});
visited.insert(temp);
}
long long sum = 0;
int count = 0;
while(count < K && !q.empty())
{
long long w = q.top().first * -1;
long long pos = q.top().second;
q.pop();
if (pos < -1e9 || pos > 1e9)
continue;
if (visited.count(pos + 1) == 0)
{
visited.insert(pos + 1);
q.push({(w + 1) * -1, pos + 1});
sum += w + 1;
count++;
}
if (visited.count(pos - 1) == 0)
{
visited.insert(pos - 1);
q.push({(w + 1) * -1, pos - 1});
sum += w + 1;
count++;
}
}
cout<<sum<<"\n";
return 0;
}