najimCseJu

Maximum Rectangular Area in a Histogram

Jan 8th, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.util.Stack;
  5. import java.util.StringTokenizer;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) throws IOException {
  10.         Reader reader = new Reader().InitConsoleReading();
  11.         int kase = reader.nextInt();
  12.         for(int ks=1;ks<=kase;ks++)
  13.         {
  14.             int n = reader.nextInt();
  15.             int[] inp = new int[n];
  16.             for(int i=0;i<n;i++)
  17.             {
  18.                 inp[i]=reader.nextInt();
  19.             }
  20.             Stack<Integer> stk = new Stack<>();
  21.             int[] L = new int[n];
  22.             for(int i=0;i<n;i++)
  23.             {
  24.                 int last = i;
  25.                 if(stk.empty() || inp[stk.peek()]<inp[i])stk.push(i);
  26.                 else
  27.                 {
  28.                     while(!stk.empty() && inp[stk.peek()]>=inp[i])
  29.                     {
  30.                         last = stk.peek();
  31.                         stk.pop();
  32.                     }
  33.                 }
  34.                 L[i] = last;
  35.                 //System.out.println(i + " " + L[i]);
  36.             }
  37.             int res = 0;
  38.             while(!stk.empty())stk.pop();
  39.             for(int i=n-1;i>=0;i--)
  40.             {
  41.                 int last = i;
  42.                 if(stk.empty() || inp[stk.peek()]<inp[i])
  43.                 {
  44.                     stk.push(i);
  45.                 }
  46.                 else
  47.                 {
  48.                     while(!stk.empty() && inp[stk.peek()]>=inp[i])
  49.                     {
  50.                         last = stk.peek();
  51.                         stk.pop();
  52.                     }
  53.                 }
  54.                 res = Math.max(res,(last - L[i]+1)*inp[i]);
  55.             }
  56.             System.out.println(res);
  57.  
  58.         }
  59.     }
  60.  
  61.     private static <T> T swap(T... args) {
  62.         // usage: z = swap(a, a=b, b=c, ... y=z);
  63.         return args[0];
  64.     }
  65.  
  66.     private static int swap(int a, int b) {
  67.         return a;
  68.     }
  69.  
  70.     static boolean next_permutation(int[] p) {
  71.         for (int a = p.length - 2; a >= 0; --a)
  72.             if (p[a] < p[a + 1])
  73.                 for (int b = p.length - 1; ; --b)
  74.                     if (p[b] > p[a]) {
  75.                         int t = p[a];
  76.                         p[a] = p[b];
  77.                         p[b] = t;
  78.                         for (++a, b = p.length - 1; a < b; ++a, --b) {
  79.                             t = p[a];
  80.                             p[a] = p[b];
  81.                             p[b] = t;
  82.                         }
  83.                         return true;
  84.                     }
  85.         return false;
  86.     }
  87.  
  88.     static int gcd(int a, int b) {
  89.         if (a < 0) a *= -1;
  90.         if (b < 0) b *= -1;
  91.         return b == 0 ? a : gcd(b, a % b);
  92.     }
  93.  
  94.     static class Reader {
  95.         public BufferedReader reader;
  96.         public StringTokenizer tokenizer;
  97.  
  98.         public Reader() {
  99.         }
  100.  
  101.         public Reader InitConsoleReading() {
  102.             reader = new BufferedReader(new InputStreamReader(System.in), 32768);
  103.             tokenizer = null;
  104.             return this;
  105.         }
  106.  
  107.         public Reader InitFileReading(String filePath) throws FileNotFoundException {
  108.             reader = new BufferedReader(new FileReader(filePath), 32768);
  109.             tokenizer = null;
  110.             return this;
  111.         }
  112.  
  113.         public String next() {
  114.             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  115.                 try {
  116.                     tokenizer = new StringTokenizer(reader.readLine());
  117.                 } catch (IOException e) {
  118.                     throw new RuntimeException(e);
  119.                 }
  120.             }
  121.             return tokenizer.nextToken();
  122.         }
  123.  
  124.         public String nextLine() throws IOException {
  125.             return reader.readLine();
  126.         }
  127.  
  128.         public int nextInt() {
  129.             return Integer.parseInt(next());
  130.         }
  131.  
  132.         public long nextLong() {
  133.             return Long.parseLong(next());
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment