|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static int A; |
| 7 | + static int B; |
| 8 | + static int totalUse; |
| 9 | + |
| 10 | + public static void main(String[] args) throws Exception { |
| 11 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 14 | + |
| 15 | + while (st.hasMoreTokens()) { |
| 16 | + A = Integer.parseInt(st.nextToken()); // 총 사용량에 기반한 요금 |
| 17 | + B = Integer.parseInt(st.nextToken()); // 요금 차이 |
| 18 | + if (A == 0 && B == 0) break; |
| 19 | + |
| 20 | + totalUse = convertToUse(A); // 총 사용량(상한) |
| 21 | + int use1 = binarySearch(0, totalUse); // 상근이의 사용 요금 |
| 22 | + int use2 = totalUse-use1; |
| 23 | + bw.write(Math.min(convertToFee(use1),convertToFee(use2)) +"\n"); |
| 24 | + st = new StringTokenizer(br.readLine()); |
| 25 | + } |
| 26 | + |
| 27 | + bw.flush(); |
| 28 | + } |
| 29 | + |
| 30 | + public static int binarySearch(int left, int right) { |
| 31 | + while (left <= right) { |
| 32 | + int mid = (left + right) / 2; |
| 33 | + int sangeunFee = convertToFee(mid); |
| 34 | + int nextToFee = convertToFee(totalUse - mid); |
| 35 | + |
| 36 | + if (Math.abs(nextToFee - sangeunFee) == B) return mid; |
| 37 | + else if (Math.abs(nextToFee - sangeunFee) < B) right = mid - 1; |
| 38 | + else left = mid + 1; |
| 39 | + } |
| 40 | + return left; |
| 41 | + } |
| 42 | + |
| 43 | + public static int convertToFee(int use) { // 사용량 -> 요금 |
| 44 | + if (use > 1000000) return 4979900 + (use - 1000000) * 7; |
| 45 | + if (use > 10000) return 29900 + (use - 10000) * 5; |
| 46 | + if (use > 100) return 200 + (use - 100) * 3; |
| 47 | + return use * 2; |
| 48 | + } |
| 49 | + |
| 50 | + public static int convertToUse(int fee) { // 요금 -> 사용량 |
| 51 | + if (fee > 4979900) return 1000000 + (fee - 4979900) / 7; |
| 52 | + if (fee > 29900) return 10000 + (fee - 29900) / 5; |
| 53 | + if (fee > 200) return 100 + (fee - 200) / 3; |
| 54 | + return fee / 2; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +``` |
0 commit comments