|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +/* |
| 5 | + * 히스토그램 |
| 6 | + */ |
| 7 | + |
| 8 | +public class DH_1725 { |
| 9 | + |
| 10 | +// ---------- 스택 사용 코드 ---------- |
| 11 | + static int[] arr; |
| 12 | + public static void main(String[] args) throws Exception { |
| 13 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + int N = Integer.parseInt(br.readLine()); |
| 15 | + |
| 16 | + arr = new int[N + 2]; |
| 17 | + // 양 끝에 0을 넣어줌 |
| 18 | + for (int i = 1; i < N + 1; i++) arr[i] = Integer.parseInt(br.readLine()); |
| 19 | + |
| 20 | + long result = getMaxArea(N); |
| 21 | + System.out.println(result); |
| 22 | + } |
| 23 | + |
| 24 | + static long getMaxArea(int N) { |
| 25 | + long maxArea = 0; |
| 26 | + Stack<Integer> stack = new Stack<>(); |
| 27 | + |
| 28 | + for (int i = 0; i < arr.length; i++) { |
| 29 | + while (!stack.isEmpty() && arr[stack.peek()] > arr[i]) { |
| 30 | + int height = arr[stack.pop()]; |
| 31 | + int width = i - (stack.isEmpty() ? 0 : stack.peek() + 1); |
| 32 | + maxArea = Math.max(maxArea, height * width); |
| 33 | + } |
| 34 | + |
| 35 | + // push 하려는 막대의 높이가 stack.peek()보다 크다면 stack에 넣어주기 |
| 36 | + stack.push(i); |
| 37 | + } |
| 38 | + |
| 39 | + return maxArea; |
| 40 | + } |
| 41 | + |
| 42 | +// ---------- 시간 초과 코드 ---------- |
| 43 | + static int[] tree, idxTree; |
| 44 | + static final int INF = Integer.MAX_VALUE; |
| 45 | + static void timeOutCode() throws Exception { |
| 46 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 47 | + int N = Integer.parseInt(br.readLine()); |
| 48 | + |
| 49 | + arr = new int[N + 2]; |
| 50 | + |
| 51 | + int k = (int) Math.ceil(Math.log(N) / Math.log(2)) + 1; |
| 52 | + tree = new int[1 << k]; |
| 53 | + idxTree = new int[1 << k]; |
| 54 | + |
| 55 | + for(int i = 1; i < N + 1; i++) arr[i] = Integer.parseInt(br.readLine()); |
| 56 | + |
| 57 | + long result = 0; |
| 58 | + for(int i = 1; i < N + 1; i++) { |
| 59 | + int l = getIdx(0, i - 1, i), r = getIdx(i + 1, N + 1, i); |
| 60 | + result = Math.max(result, (r - l - 1) * arr[i]); |
| 61 | + } |
| 62 | + |
| 63 | + System.out.println(result); |
| 64 | + } |
| 65 | + |
| 66 | + static int getIdx(int s, int e, int c) { |
| 67 | + // 인덱스가 s와 e 사이이면서 현재(c)보다 작은 숫자를 가진 원소의 idx 구하기 |
| 68 | + boolean isLeft = e <= c; |
| 69 | + int k = (int) Math.ceil(Math.log(e - s + 1) / Math.log(2)) + 1; |
| 70 | + |
| 71 | + int idx = 1 << (k - 1); |
| 72 | + for(int i = s; i < e + 1; i++) { |
| 73 | + tree[idx + i - s] = arr[i]; |
| 74 | + |
| 75 | + // 인덱스 저장 (idxTree에서 0인 것과 0번 인덱스를 구분하기 위해, 진짜 인덱스에 + 1을 해줌) |
| 76 | + idxTree[idx + i - s] = i + 1; |
| 77 | + } |
| 78 | + |
| 79 | + if(isLeft) { |
| 80 | + |
| 81 | + for(int i = idx + e + 1 - s; i < idxTree.length; i++) { |
| 82 | + tree[i] = tree[i - 1]; |
| 83 | + idxTree[i] = idxTree[i - 1]; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + for(int i = idx - 1; i > 0; i--) { |
| 88 | + int a = i * 2; |
| 89 | + int b = i * 2 + 1; |
| 90 | + |
| 91 | + if(tree[a] < arr[c] && tree[b] < arr[c]) { |
| 92 | + if(isLeft) { |
| 93 | + tree[i] = tree[b]; |
| 94 | + idxTree[i] = idxTree[b]; |
| 95 | + } else { |
| 96 | + tree[i] = tree[a]; |
| 97 | + idxTree[i] = idxTree[a]; |
| 98 | + } |
| 99 | + } else if(tree[a] < arr[c] && tree[b] >= arr[c]) { |
| 100 | + tree[i] = tree[a]; |
| 101 | + idxTree[i] = idxTree[a]; |
| 102 | + } else if(tree[a] >= arr[c] && tree[b] < arr[c]) { |
| 103 | + tree[i] = tree[b]; |
| 104 | + idxTree[i] = idxTree[b]; |
| 105 | + } else { |
| 106 | + tree[i] = tree[a]; |
| 107 | + if(isLeft) idxTree[i] = Math.min(idxTree[a], idxTree[b]); |
| 108 | + else idxTree[i] = Math.max(idxTree[a], idxTree[b]); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return idxTree[1] - 1; |
| 113 | + } |
| 114 | +} |
0 commit comments