Advertisement
moonelite99

Untitled

Feb 21st, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Stack;
  3.  
  4. public class Main2{
  5.     public static int Rect(int[] a){
  6.         int r = 0;
  7.             int area;
  8.             int vt;
  9.             Stack<Integer> s = new Stack<>();
  10.             int i = 0;
  11.             while(i < a.length){
  12.                 if(s.empty() || a[s.peek()] <= a[i]){
  13.                     s.push(i++);
  14.                 }
  15.                 else if (!s.empty()){
  16.                     vt = s.peek();
  17.                     s.pop();
  18.                     area = a[vt] * (s.empty() ? i : i - s.peek() - 1);
  19.                     r = Math.max(r, area);
  20.                 }
  21.             }
  22.             while(!s.empty()){
  23.                 vt = s.peek();
  24.                 s.pop();
  25.                 area = a[vt] * (s.empty() ? i : i - s.peek() - 1);
  26.                 r = Math.max(r, area);
  27.             }
  28.             return r;
  29.     }
  30.     public static void main(String[] args) {
  31.         Scanner sc = new Scanner(System.in);
  32.         while(true){
  33.             int n = sc.nextInt();
  34.             if (n == 0) break;
  35.             int[] a = new int[n];
  36.             for (int i = 0; i < n; i++) {
  37.                 a[i] = sc.nextInt();
  38.             }
  39.             System.out.println(Rect(a));
  40.         }
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement