Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2e3a9d9

Browse files
committed
배수빈: [BOJ] 18513 샘터_250326
1 parent 0d5ac1a commit 2e3a9d9

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

‎BOJ/15001-20000번/SB_18513.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayDeque;
5+
import java.util.Deque;
6+
import java.util.HashSet;
7+
import java.util.StringTokenizer;
8+
9+
public class SB_18513 {
10+
static int N, K;
11+
static Deque<Node> que = new ArrayDeque<>();
12+
static HashSet<Integer> set = new HashSet<>();
13+
static int[] dir = new int[]{-1, 1};
14+
15+
private static long bfs() {
16+
long cnt = 0;
17+
18+
int check = 0;
19+
while (!que.isEmpty()) {
20+
Node cur = que.poll();
21+
22+
for (int d : dir) {
23+
int nxt = cur.i + d;
24+
if (set.contains(nxt)) continue;
25+
cnt+= cur.v+1;
26+
set.add(nxt);
27+
que.offer(new Node(nxt, cur.v + 1));
28+
check++;
29+
30+
if (check==K) return cnt;
31+
}
32+
}
33+
return cnt;
34+
}
35+
36+
public static void main(String[] args) throws IOException {
37+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
38+
StringTokenizer st = new StringTokenizer(br.readLine());
39+
N = Integer.parseInt(st.nextToken());
40+
K = Integer.parseInt(st.nextToken());
41+
42+
st = new StringTokenizer(br.readLine());
43+
for (int i = 0; i < N; i++) {
44+
int idx = Integer.parseInt(st.nextToken());
45+
que.offer(new Node(idx, 0));
46+
set.add(idx);
47+
}
48+
49+
System.out.println(bfs());
50+
}
51+
}
52+
53+
class Node {
54+
int i, v;
55+
public Node(int i, int v) {
56+
this.i = i;
57+
this.v = v;
58+
}
59+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /