개발자 톡
연습문제 톡
[HSAT 4회 정기 코딩 인증평가 기출] 슈퍼컴퓨터 클러스터
java 정답 코드
- 등록일
- 2025-01-27 15:37:15
- 조회수
- 127
- 작성자
- tjsqls2067
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int N = sc.nextInt(); final BigInteger B = new BigInteger(sc.next());
sc.nextLine();
String[] inputArr = sc.nextLine().split(" ");
long[] cptPerfArr = new long[N];
for(int i = 0; i < N; i++){
cptPerfArr[i] = Long.parseLong(inputArr[i]);
}
// 1. N개의 컴퓨터에 대해, 초기 ai 값에 대해 동일 개수를 세어 TreeMap에 저장
TreeMap<Long, Long> cptPerfMap = new TreeMap<>();
for(int i = 0; i < N; i++){
Long cptPerf = cptPerfArr[i];
if(cptPerfMap.containsKey(cptPerf)){
cptPerfMap.put(cptPerf,cptPerfMap.get(cptPerf) + 1L);
}else{
cptPerfMap.put(cptPerf,1L);
}
}
// 1-1. TreeMap의 0번째 해당하는 값을 최저 성능값(minPerf)로 지정
long minPerf = cptPerfMap.firstKey();
long start = cptPerfMap.firstKey();
long end = cptPerfMap.lastKey() + 1_000_000_000L;
// 1-3. perf = 초기 최저성능값
long perf = 0;
// 2. start >= end 동안 반복문 수행
while(start < end){
perf = start + ((end - start) / 2);
// 3. TreeMap 에서 perf 이하의 범위의 노드들에대해 다음 연산 수행
long fromKey = cptPerfMap.firstKey();
long toKey = perf;
BigInteger sum = new BigInteger("0");
for(Map.Entry<Long,Long> entry : cptPerfMap.subMap(fromKey, true, toKey, true).entrySet()){
// 3-1. 범위 내 노드들에 대해, ((perf - key) ^ 2 ) * value 하여 sum
BigInteger dx = new BigInteger(Long.toString(perf - entry.getKey()));
BigInteger dxSqure = dx.multiply(dx);
BigInteger dxSqures = dxSqure.multiply(new BigInteger(Long.toString(entry.getValue())));
sum = sum.add(dxSqures);
}
// 3-2. sum값이 B 이하이면, perf 탐색 범위값 올림
if(sum.compareTo(B) <= 0){
minPerf = Math.max(minPerf, perf);
start = perf + 1;
}else{
// 3-3. sum값이 B 이상이며, perf 탐색 범위값 내림
end = perf;
}
}
System.out.println(minPerf);
}
}
#[HSAT_4회_정기_코딩_인증평가_기출]_슈퍼컴퓨터_클러스터