|
| 1 | +```java |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + |
| 10 | + |
| 11 | + static int blockCnt; |
| 12 | + static long ans; |
| 13 | + static int[] blocks; |
| 14 | + static PriorityQueue<Block> pq = new PriorityQueue<>(); |
| 15 | + static HashSet<Integer> nums = new HashSet<>(); |
| 16 | + static PriorityQueue<Integer> numPq = new PriorityQueue<>(); |
| 17 | + |
| 18 | + static class Block implements Comparable<Block>{ |
| 19 | + int start; |
| 20 | + int height; |
| 21 | + |
| 22 | + public Block (int start, int height) { |
| 23 | + this.start = start; |
| 24 | + this.height = height; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public int compareTo(Block b) { |
| 29 | + return Integer.compare(b.height, this.height); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + public static void main(String[] args) throws NumberFormatException, IOException { |
| 35 | + init(); |
| 36 | + process(); |
| 37 | + print(); |
| 38 | + } |
| 39 | + |
| 40 | + public static void init() throws NumberFormatException, IOException { |
| 41 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 42 | + blockCnt = Integer.parseInt(br.readLine()); |
| 43 | + blocks = new int[blockCnt]; |
| 44 | + ans = 0; |
| 45 | + for (int i = 0; i < blockCnt; i++) { |
| 46 | + blocks[i] = Integer.parseInt(br.readLine()); |
| 47 | + nums.add(blocks[i]); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + public static void process() { |
| 54 | + ans = findLargest(0,blockCnt-1); |
| 55 | + |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + public static long findLargest(int start, int end) { |
| 60 | + |
| 61 | + if (start == end) return blocks[start]; |
| 62 | + |
| 63 | + int mid = (start+end)/2; |
| 64 | + long leftMax = findLargest(start,mid); |
| 65 | + long rightMax = findLargest(mid+1,end); |
| 66 | + long max = Math.max(leftMax, rightMax); |
| 67 | + |
| 68 | + int leftIdx = mid; |
| 69 | + int rightIdx = mid+1; |
| 70 | + long curNum = Math.min(blocks[leftIdx], blocks[rightIdx]); |
| 71 | + long curMax =curNum*(rightIdx-leftIdx+1); |
| 72 | + |
| 73 | + while(true) { |
| 74 | + |
| 75 | + if (leftIdx == start && rightIdx == end) break; |
| 76 | + int nLeftNum = 0; |
| 77 | + int nRightNum = 0; |
| 78 | + if (start < leftIdx) nLeftNum = blocks[leftIdx-1]; |
| 79 | + if (rightIdx < end) nRightNum = blocks[rightIdx+1]; |
| 80 | + |
| 81 | + if (nLeftNum <= nRightNum && rightIdx != end) { |
| 82 | + rightIdx++; |
| 83 | + } else { |
| 84 | + leftIdx--; |
| 85 | + } |
| 86 | + |
| 87 | + curNum = Math.min(curNum, blocks[rightIdx]); |
| 88 | + curNum = Math.min(curNum, blocks[leftIdx]); |
| 89 | + curMax = Math.max(curMax, curNum*(rightIdx-leftIdx+1)); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + max = Math.max(max, curMax); |
| 94 | + |
| 95 | + return max; |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + public static void print() { |
| 102 | + System.out.println(ans); |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
0 commit comments