Advertisement
WadeRollins2710

The Correct Largest Rectangle

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