Fahim_7861

Histogram Maximum Rectangular Area in a Histogram

Feb 19th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1.  
  2. #include<bits/stdc++.h>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. #include <ext/pb_ds/assoc_container.hpp>
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7. typedef int ll;
  8. typedef unsigned long long ull;
  9. typedef pair<ll,ll>pll;
  10. typedef pair<int,int>pii;
  11. //typedef pair<int,pair<int,int>>piii;
  12. //typedef pair<ll,pair<ll,ll>>plll;
  13. //typedef tree<ll, null_type, less<ll>, rb_tree_tag,tree_order_statistics_node_update> orderedSet;
  14. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
  15. #define sf(a) scanf("%I64d",&a)
  16. #define pf(a) printf("%I64d\n",a)
  17. #define mem(a,b) memset(a,b,sizeof(a))
  18. #define vll(v) v.begin(),v.end()
  19. #define all(x) x.rbegin(),x.rend()
  20. #define min3(a, b, c) min(a, min(b, c))
  21. #define F first
  22. #define S second
  23. #define minheap int,vector<int>,greater<int>
  24. //#define mp make_pair
  25. #define pb push_back
  26. #define pp pop_back
  27. #define eb emplace_back
  28. #define in freopen("input.txt", "r", stdin)
  29. #define out freopen("output.txt", "w", stdout)
  30. #define BOUNDARY(i, j) ((i >= 0 && i < row) && (j >= 0 && j < column))
  31. #define ischar(x) (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
  32. #define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
  33. const int Max = 1005;
  34. const int Mod = 1e9 + 7;
  35. const double PI =3.141592653589793238463;
  36. bool compare(const pair<int,int> &a, const pair<int,int> &b)
  37. {
  38. return (a.first > b.first);
  39. }
  40. ll lcm(ll a,ll b)
  41. {
  42. if(a==0 || b==0)return 0;
  43.  
  44. return a/__gcd(a,b)*b;
  45. }
  46. //___________________________________________________________________________________________________________________
  47. // CODE STARTS FROM HERE
  48. // MU_Codefighter2019
  49. //-------------------------------------------------------------------------------------------------------------------
  50.  
  51. int getMaxArea(int hist[], int n)
  52. {
  53.  
  54. stack<int> s;
  55.  
  56. int max_area = 0; // Initialize max area
  57. int tp; // To store top of stack
  58. int area_with_top; // To store area with top bar
  59. // as the smallest bar
  60.  
  61. int i = 0;
  62. while (i < n)
  63. {
  64. if (s.empty() || hist[s.top()] <= hist[i])
  65. s.push(i++);
  66.  
  67. else
  68. {
  69. tp = s.top(); // store the top index
  70. s.pop(); // pop the top
  71.  
  72. area_with_top = hist[tp] * (s.empty() ? i :
  73. i - s.top() - 1);
  74.  
  75. // cout<<area_with_top<<endl;
  76. // update max area, if needed
  77. if (max_area < area_with_top)
  78. max_area = area_with_top;
  79. }
  80. }
  81.  
  82.  
  83. while (s.empty() == false)
  84. {
  85. tp = s.top();
  86. s.pop();
  87. area_with_top = hist[tp] * (s.empty() ? i :
  88. i - s.top() - 1);
  89.  
  90. if (max_area < area_with_top)
  91. max_area = area_with_top;
  92. }
  93.  
  94. return max_area;
  95. }
  96.  
  97.  
  98. int main()
  99. {
  100. fastread();
  101.  
  102. ll t,cas=1;
  103.  
  104. cin>>t;
  105.  
  106. while(t--)
  107. {
  108. ll n,ans=0,m;
  109.  
  110. cin>>n>>m;
  111.  
  112. ll hist[m+1]={0},j,a;
  113.  
  114. string str;
  115.  
  116. for(ll i=0; i<n; i++)
  117. {
  118. cin>>str;
  119.  
  120. for(j=0; j<m; j++)
  121. {
  122.  
  123. a=str[j]-'0';
  124. if(a)hist[j]=0;
  125.  
  126. else hist[j]++;
  127. }
  128.  
  129. ans=max(ans,getMaxArea(hist,m));
  130. }
  131.  
  132. cout<<"Case "<<cas++<<": "<<ans<<endl;
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment