그리디 알고리즘의 아주 기초적인 문제였다.
1. 인덱스와 소요시간을 저장하는 클래스 Person을 생성함
2. Person의 ArrayList를 생성한 뒤 갑을 입력해줌
3. ArrayList를 소요시간순으로 정렬함
4. 정렬된 ArrayList를 이용해 소요시간의 누적합을 구하고 출력함
import java.io.*;
import java.util.*;
public class Main {
static class Person{
int idx;
int time;
Person(int idx, int time){
this.idx = idx;
this.time = time;
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine()," ");
ArrayList<Person> list = new ArrayList<>();
int total = 0;
for(int i=1;i<=N;i++) {
list.add(new Person(i, Integer.parseInt(st.nextToken())));
}
list.sort((o1, o2)->{
return o1.time-o2.time;
});
for(int i=0;i<N;i++) {
for(int j=0;j<=i;j++) {
total += list.get(j).time;
}
}
System.out.println(total);
}
}
'Algorithm > [BOJ] - JAVA' 카테고리의 다른 글
[백준] 2875 : 대회 or 인턴 JAVA 풀이 (0) | 2022.10.30 |
---|---|
[백준] 11047 : 동전 0 JAVA 풀이 (0) | 2022.10.28 |
[백준] 1707 : 이분 그래프 JAVA 풀이 (0) | 2022.10.28 |
[백준] 9466 : 팀 프로젝트 JAVA 풀이 (0) | 2022.10.25 |
[백준] 1167 : 트리의 지름 JAVA 풀이 (0) | 2022.10.25 |