[BOJ] 백준 [1826] 연료채우기 JAVA

[BOJ] 백준 [1826] 연료채우기 JAVA

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

import java.util. * ; import java.io. * ; import java.util.stream. * ; public class Main { static Queue < int [] > q; static Queue < Integer > fuelQ; static int town,curFuel; public static void main( String [] args) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader( System . in )); int n = Integer. parseInt (br.readLine()); q = new PriorityQueue < > ((o1, o2) - > o1[ 0 ] - o2[ 0 ]); // 거리순 heap fuelQ = new PriorityQueue < > (Collections.reverseOrder()); for ( int i = 0 ;i < n;i + + ) q. add (Arrays.stream(br.readLine(). split ( " " )) .mapToInt(Integer:: parseInt ).toArray()); int [] input = Arrays.stream(br.readLine(). split ( " " )) .mapToInt(Integer:: parseInt ).toArray(); town = input[ 0 ]; curFuel = input[ 1 ]; System . out . println (getCount()); } static int getCount(){ int ans = 0 ; while (curFuel < town){ while ( ! q.isEmpty() & & q.peek()[ 0 ] < = curFuel) fuelQ. add (q.poll()[ 1 ]); if (fuelQ.isEmpty()) return - 1 ; ans + + ; curFuel + = fuelQ.poll(); } return ans; } } Colored by Color Scripter

from http://katastrophe.tistory.com/97 by ccl(A) rewrite - 2021-12-03 19:01:57