
우선순위 큐를 사용하여 내부 요소를 자동 정렬 시킨다.(내림차순으로)
따라서 제일 앞에 있는 요소는 큐 내부에서 가장 높은 수가 됨.
while
안에서 다솜의 득표수가 가장 클때까지 진행if
가 없었을 때 후보 1명이 처리가 안되어 런타임 에러 발생.후보가 1명일 경우 위해서 if 문 추가함.
풀이
public class Main_1417 {
public static void main(String[] args) throws IOException {
//국회의원 선거
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
//사람 수
int N = Integer.parseInt(br.readLine());
int dasom = Integer.parseInt(br.readLine());
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for(int i = 0; i< N-1; i++) {
pq.add(Integer.parseInt(br.readLine()));
}
int count = 0;
if (N == 1) {
bw.write(String.valueOf(count));
bw.flush();
bw.close();
return;
}
while(dasom<=pq.peek()) {
int top = pq.poll();
pq.add(top - 1);
count++;
dasom++;
}
bw.write(String.valueOf(count));
bw.flush();
bw.close();
}
}
Share article