Movatterモバイル変換


[0]ホーム

URL:


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

Commit58faaf8

Browse files
authored
Update 3.java
1 parent7fb0e9b commit58faaf8

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎15/3.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
importjava.util.*;
2+
3+
publicclassMain {
4+
5+
publicstaticvoidmain(String[]args) {
6+
// 집의 개수(N)와 공유기의 개수(C)를 입력받기
7+
Scannersc =newScanner(System.in);
8+
intn =sc.nextInt();
9+
intc =sc.nextInt();
10+
11+
// 전체 집의 좌표 정보를 입력받기
12+
ArrayList<Integer>arr =newArrayList<>();
13+
for (inti =0;i <n;i++) {
14+
arr.add(sc.nextInt());
15+
}
16+
17+
// 이진 탐색을 위해 정렬 수행
18+
Collections.sort(arr);
19+
20+
intstart =arr.get(1) -arr.get(0);// 집의 좌표 중에 가장 작은 값
21+
intend =arr.get(n -1) -arr.get(0);// 집의 좌표 중에 가장 큰 값
22+
intresult =0;
23+
24+
while (start <=end) {
25+
// mid는 가장 인접한 두 공유기 사이의 거리(Gap)을 의미
26+
intmid = (start +end) /2;
27+
intvalue =arr.get(0);
28+
intcnt =1;
29+
// 현재의 mid 값을 이용해 공유기를 설치하기
30+
for (inti =1;i <n;i++) {// 앞에서부터 차근차근 설치
31+
if (arr.get(i) >=value +mid) {
32+
value =arr.get(i);
33+
cnt +=1;
34+
}
35+
}
36+
// C개 이상의 공유기를 설치할 수 있는 경우, 거리를 증가시키기
37+
if (cnt >=c) {
38+
start =mid +1;
39+
result =mid;// 최적의 결과를 저장
40+
}
41+
// C개 이상의 공유기를 설치할 수 없는 경우, 거리를 감소시키기
42+
else {
43+
end =mid -1;
44+
}
45+
}
46+
System.out.println(result);
47+
}
48+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp