Advertisement
WadeRollins2710

Largest Rectangle

Mar 25th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int maxSizeFromHistogram(int a[], int n)
  6. {
  7.     int stack[n];
  8.     int _max = 0;
  9.     int length = 0;
  10.     int tp;
  11.     int areaWithTop;
  12.  
  13.     int i = 0;
  14.     while (i < n) {
  15.         if (length == 0 || stack[length] <= a[i]) {
  16.             stack[length] = i; length++; i++;
  17.         }
  18.         else {
  19.             tp = stack[length - 1]; length--;
  20.             int w = (length == 0) ? i : i - stack[length - 1] - 1;
  21.             areaWithTop = a[tp] * w;
  22.  
  23.             if (_max < areaWithTop)
  24.                 _max = areaWithTop;
  25.         }
  26.     }
  27.  
  28.     while (length != 0) {
  29.         tp = stack[length - 1]; length--;
  30.         int w = (length == 0) ? i : i - stack[length - 1] - 1;
  31.         areaWithTop = a[tp] * w;
  32.  
  33.         if (_max < areaWithTop)
  34.             _max = areaWithTop;
  35.     }
  36. }
  37.  
  38. int main()
  39. {
  40.     int n; cin >> n;
  41.     int a[n];
  42.     for (int i = 0; i < n; i++)
  43.         a[i] = 0;
  44.     int _max = 0;
  45.     for (int rowIndex = 0; rowIndex < n; rowIndex++)
  46.     {
  47.         for (int j = 0; j < n; j++)
  48.         {
  49.             int value; cin >> value;
  50.             if (value == 0) a[j]++;
  51.             else a[j] = 0;
  52.         }
  53.  
  54.         int result = maxSizeFromHistogram(a, n);
  55.  
  56.         if (_max < result) _max = result;
  57.     }
  58.     cout << "Result: " << _max;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement