前置问题:51nod 1102 面积最大的矩形

附上链接:

51nod 1102 面积最大的矩形

这题的题解博客

需要了解的知识:单调栈,在前置问题中已经讲解。

解题思路

  1. 对每行求左边连续1的个数,得到数组a[i][j];
  2. 对于第j列,找出每个位置i的数字a[i][j]上面第一个比它小数字l,和下面第一个比它小的数字r。
  3. 由这个点所在列为底,这个点的数字为最小值产生的矩形的面积为a[i][j]*(r-l-1),用这一列每一个面积更新ans。
  4. 上面2的求法就是单调栈了,总时间复杂度o(n*m)。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[510][510];
int l[510],r[510];
int main(){
ios::sync_with_stdio(false);
int m,n;
cin >> m >> n;
for(int i = 1;i <= m; ++i){
for(int j = 1;j <= n; ++j){
cin >> a[i][j];
if(a[i][j] == 1) a[i][j] += a[i][j-1];
}
}
int ans = 0;
for(int i = 1;i <= n; ++i){
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
stack<int> s;
s.push(1);
a[0][i] = a[m+1][i] = -1;
for(int j = 2;j <= m+1; ++j){
while(s.size() and a[j][i] < a[s.top()][i]){
r[s.top()] = j;
s.pop();
}
s.push(j);
}
while(s.size()) s.pop();
s.push(m);
for(int j = m-1;j >= 0; --j){
while(s.size() and a[j][i] < a[s.top()][i]){
l[s.top()] = j;
s.pop();
}
s.push(j);
}
for(int j = 1;j <= m; ++j){
ans = max(ans, (r[j]-l[j]-1)*a[j][i]);
}
}
cout << ans << endl;
return 0;
}

最新文章

  1. 2013成都网络赛 C We Love MOE Girls(水题)
  2. ASP.NET常见面试题及答案(130题)
  3. u-boot平台的建立,驱动的添加,索引的创建,命令机制的实现.
  4. 最喜欢的算法(们) - Levenshtein distance
  5. Debugging WebLogic Server Applications Using Eclipse and the WebLogic-Plugin
  6. zoeDylan.js框架-数据底层
  7. Reactivecocoa初级使用
  8. PHP字符串操作汇总
  9. usaco 地震 &amp;&amp; 奶牛观光
  10. Android WebView JavaScript交互
  11. LeetCode——Majority Element
  12. 27.Linux-DM9000C网卡移植(详解)
  13. Flag
  14. bzoj 1925: [Sdoi2010]地精部落
  15. js 格林威治时间转正常格式并兼容ios
  16. Spring中Model、ModelMap及ModelAndView之间的区别
  17. HADOOP高可用机制
  18. 002_监测ssl证书过期时间
  19. ImageMagick: DrawImage(Image*,DrawInfo*) 绘制填充图片时卡住的原因分析
  20. https://scrapingclub.com/exercise/basic_captcha/

热门文章

  1. CentOS 6.7操作系统安装
  2. php开启CURL支持
  3. CSS实现栅格布局
  4. BroadcastReceiver register 广播的动态注册方式
  5. C++ 简单内存泄漏检测方法
  6. 记录sql执行顺序
  7. day09-3 数据类型总结,深浅拷贝
  8. Python中zip()与zip(*)的用法
  9. Android 7.0 Gallery图库源码分析2 - 分析启动流程
  10. 在使用easyui datagrid在tab中遇到的问题