https://www.acmicpc.net/problem/11399
11399번: ATM
첫째 줄에 사람의 수 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄에는 각 사람이 돈을 인출하는데 걸리는 시간 Pi가 주어진다. (1 ≤ Pi ≤ 1,000)
www.acmicpc.net
그리디알고리즘입니다.
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; class Withdrawer implements Comparable<Withdrawer>{ private int time; public Withdrawer(int time) { this.time = time; } public int getTime() { return this.time; } public void setTime(int time) { this.time = time; } @Override public int compareTo(Withdrawer other) { return this.time - other.time; } @Override public String toString() { return "toString:"+this.time; } } public class Main { public static int N; public static List<Withdrawer> withdrawerlist = new ArrayList<Withdrawer>(); public static int prev=0,sum=0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); N = sc.nextInt(); for(int i=0;i<N;i++) { withdrawerlist.add(new Withdrawer(sc.nextInt())); } Collections.sort(withdrawerlist); for(int i=0;i<N;i++) { prev += withdrawerlist.get(i).getTime(); sum +=prev; } System.out.println(sum); } }
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 12100 2048(Easy) - 시뮬레이션(Simulation) + 브루트포스(brute force) JAVA (0) | 2022.01.10 |
---|---|
[백준] 13305번 주유소 - 그리디(Greedy, 탐욕법) JAVA (0) | 2021.12.23 |
[백준] 1541번 잃어버린 괄호 - 문자열 파싱(Parsing) + Stack(스택) + 그리디(탐욕법, Greedy) JAVA (0) | 2021.12.22 |
[백준] 1931 회의실배정 - 탐욕법(Greedy) + 정렬(Sort) JAVA (0) | 2021.12.19 |
[백준] 11047번 - 동전 0 (0) | 2021.12.17 |