uva12462 Rectangle题解
来源:互联网 发布:subversion mac 编辑:程序博客网 时间:2024/06/07 00:12
题目大意:
有n(<=10^5)个矩形依次相邻,m(<=30)种颜色。第i个矩形高度h[i],宽度为1,颜色为c[i]。求一个面积最大的大矩形,要求大矩形是由这些矩形组成,且大矩形中包含m种颜色。
可以的:
不可以的:
题解:
可以说是广告印刷加强版
对与每个h[i],单调队列找左极限和右极限
对于要求区间每种颜色都有,可以开一个color[i][j]数组表示到i为止j颜色个数,最后枚举时只需要判定是否对于所有的j(1<=j<=m)是否满足color[r[i]][j]-color[l[i]-1][j]>0.
时间复杂度O(n*m)
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;const int maxn=100100;long long color[maxn][35];int q[maxn];long long l[maxn],r[maxn],h[maxn];int n,m,tail;int main(){ while(scanf("%d%d",&n,&m),n||m) { for(int i=1;i<=n;i++) scanf("%lld",&h[i]); h[0]=0;h[n+1]=0; for(int i=0;i<=n;i++) for(int j=0;j<=m;j++) color[i][j]=0; for(int i=1;i<=n;i++) { int x;scanf("%d",&x);x++; for(int j=1;j<=m;j++) color[i][j]=color[i-1][j]+(j==x); } //pre tail=0;q[0]=0; for(int i=1;i<=n;i++) { while(h[i]<=h[q[tail]])tail--; l[i]=q[tail]+1; tail++;q[tail]=i; } tail=0;q[0]=n+1; for(int i=n;i>=1;i--) { while(h[i]<=h[q[tail]])tail--; r[i]=q[tail]-1; tail++;q[tail]=i; } long long ans=0; for(int i=1;i<=n;i++) { long long s=l[i],t=r[i]; bool flag=true; for(int j=1;j<=m;j++) if(color[t][j]-color[s-1][j]<=0){flag=false;break;} if(flag && (t-s+1)*h[i]>ans)ans=(t-s+1)*h[i]; } printf("%lld\n",ans); } return 0;}
- uva12462 Rectangle题解
- LeetCode - Maximal Rectangle 题解
- LeetCode题解:Rectangle Area
- LeetCode85 Maximal Rectangle java题解
- LeetCode 题解(193) : Rectangle Area
- LeetCode题解:Construct the Rectangle
- leetcode_题解_Largest Rectangle in Histogram
- LeetCode - Largest Rectangle in Histogram 题解
- LeetCode 题解(100): Maximal Rectangle
- LeetCode84 Largest Rectangle in Histogram java题解
- 【E】【51】【leetcode题解】Rectangle Area
- leetcode题解分析_85. Maximal Rectangle
- leetcode题解分析_223. Rectangle Area
- leetcode题解-84. Largest Rectangle in Histogram
- Lintcode122 Largest Rectangle In Histogram solution 题解
- leetcode题解-84.Largest Rectangle in Histogram
- LeetCode 题解(99): Largest Rectangle in Histogram
- LeetCode 题解(284) : Smallest Rectangle Enclosing Black Pixels
- swun 1429&hdu 4320 进制转化(大牛&小牛代码)
- 多库俄罗斯方块之十一(Objective-c)
- qq button
- APC常量定义与PHP的define比较
- JSON 是 JavaScript 原生格式 以及引申出来的思考
- uva12462 Rectangle题解
- Sql server中的主键与外键
- PHP支持组操作的Memcache类(域操作)
- 使用MongoDB开发实践体会
- /*DEVICE_BUILTIN*/
- webservice学习之AXIS初学笔记--创建自己的webservice
- 动态解析文件夹下所有的压缩文件
- 把一个字符串通过递归转换成相应数字
- 北大ACM poj 3991