Advertisement
unknown_0711

Untitled

Oct 30th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Main {
  6. public static void main(String[] args) throws java.lang.Exception {
  7. Scanner sc = new Scanner(System.in);
  8. long n = sc.nextLong();
  9. long a[] = new long[(int) n];
  10. for (int i = 0; i < (int) n; i++) {
  11. a[i] = sc.nextLong();
  12. }
  13.  
  14. System.out.print(ar(a, n));
  15.  
  16. }
  17.  
  18. public static long ar(long hist[], long n) {
  19.  
  20. Stack<Integer> st1 = new Stack<>();
  21. Stack<Integer> st2 = new Stack<>();
  22. int h1[] = new int[(int) n];
  23. int h2[] = new int[(int) n];
  24. int k = -1;
  25.  
  26. for (int i = (int) n - 1; i >= 0; i--) {
  27.  
  28. while (!st1.isEmpty() && hist[st1.peek()] >= hist[i])
  29. st1.pop();
  30.  
  31. k = i;
  32.  
  33. if (st1.isEmpty())
  34. h1[i] = (int) n;
  35.  
  36. else
  37. h1[i] = st1.peek();
  38.  
  39. st1.push(k);
  40.  
  41. }
  42.  
  43. for (int i = 0; i < (int) n; i++) {
  44.  
  45. while (!st2.isEmpty() && hist[st2.peek()] >= hist[i])
  46. st2.pop();
  47.  
  48. k = i;
  49.  
  50. if (st2.isEmpty())
  51. h2[i] = -1;
  52.  
  53. else
  54. h2[i] = st2.peek();
  55.  
  56. st2.push(k);
  57.  
  58. }
  59.  
  60. long mx = 0;
  61. for (int i = 0; i < n; i++) {
  62. if (hist[i] * (h1[i] - h2[i] - 1) > mx) {
  63. mx = hist[i] * (h1[i] - h2[i] - 1);
  64. }
  65. }
  66. return mx;
  67.  
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement