HDU 1505 Largest Rectangle in a Histogram && HDU 1506 City Game(动态规划)

1506题意:给你连续的直方图(底边边长为1),求连续的矩阵面积。

对每个直方图,分别向左向右进行扩展。

#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<map>
#include<cmath>
#include<iostream>
#include <queue>
#include <stack>
#include<algorithm>
#include<set>
using namespace std;
#define INF 1e8
#define eps 1e-8
#define LL long long
#define N 100010
#define mol 1000000007
int i,n,t;
LL a[N],l[N],r[N],Max;
int main()
{
    while (scanf("%d",&n) &&  n)
    {
        for (i=1; i<=n; ++i) scanf("%I64d",&a[i]);
        l[1]=1;
        r[n]=n;
        for(i=2;i<=n;i++)
        {
            t=i;
            while(t>1&&a[i]<=a[t-1])//从左往右向左扩展
                t=l[t-1];
            l[i]=t;
        }
        for(i=n-1;i>=1;i--)
        {
            t=i;
            while(t<n&&a[i]<=a[t+1])//从右往左向右扩展
                t=r[t+1];
            r[i]=t;
        }
        Max=0;
        for (i=1; i<=n; ++i)
        {
            if ((r[i]-l[i]+1)*a[i]>Max)
                Max=(r[i]-l[i]+1)*a[i];
        }
        printf("%I64d\n",Max);
    }
    return 0;
}  

1505题意:求最大零(F)矩阵,1506加强版,把2维转换化成以每一行底,组成的最大面积

#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<map>
#include<cmath>
#include<iostream>
#include <queue>
#include <stack>
#include<algorithm>
#include<set>
using namespace std;
#define INF 1e8
#define eps 1e-8
#define LL long long
#define maxn 1001
#define mol 1000000007
int t,n,m,a[maxn][maxn],l[maxn][maxn],r[maxn][maxn];
char s[maxn];
int main()
{

	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		int i,j;
		memset(a,0,sizeof(a));
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				scanf("%s",s);
				if(s[0]=='F') a[i][j]=a[i-1][j]+1;
				else a[i][j]=0;
			}
		}
		//printf("\n");
		int ans=0;
		for(i=1;i<=n;i++)
		{
			int t;
			l[i][1]=1;r[i][m]=m;
			for(j=2;j<=m;j++)
			{
				t=j;
				while(t>1&&a[i][j]<=a[i][t-1])
					t=l[i][t-1];
				l[i][j]=t;
			}
			for(j=m-1;j>0;j--)
			{
				t=j;
				while(t<m&&a[i][j]<=a[i][t+1])
					t=r[i][t+1];
				r[i][j]=t;
			}
			for(j=1;j<=m;j++)
			{
				if(a[i][j]*(r[i][j]-l[i][j]+1)>ans)
					ans=a[i][j]*(r[i][j]-l[i][j]+1);
			}
		}
		printf("%d\n",ans*3);
	}
	return 0;
}
/*
2
5 6
R F F F F F
F F F F F F
R R R F F F
F F F F F F
F F F F F F

5 5
R R R R R
R R R R R
R R R R R
R R R R R
R R R R R
*/

HDU 1505 Largest Rectangle in a Histogram && HDU 1506 City Game(动态规划),布布扣,bubuko.com

时间: 2024-08-02 02:46:31

HDU 1505 Largest Rectangle in a Histogram && HDU 1506 City Game(动态规划)的相关文章

HDU 1056 Largest Rectangle in a Histogram(dp)(求最大的矩形面积)

Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of

Largest Rectangle in a Histogram HDU - 1506 (单调栈)

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the

暑期训练狂刷系列——Hdu 3506 Largest Rectangle in a Histogram (单调栈)

题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目大意: 给出一个数列An,问以Ai为最小值的区间内有多少个元素? 解题思路: 手动模拟一个栈.栈内元素为一个单调不递减序列.当新元素Ai需要进栈,如果栈顶元素大于Ai,弹出栈顶元素,直到栈顶元素不大于Ai,Ai进栈. 这里可以保证i是高为栈顶元素的矩形向后延伸的边界,弹出过程中记录高为Ai的矩阵向前延伸的边界. 1 //#include <bits/stdc++.h> 2 #inclu

hdu 1507 Largest Rectangle in a Histogram 动态规划计算最大面积

记录动态规划dpl,dpr,分辨记录i左面的比i大的,右面比i大的,然后(dpr[i]-dpl[i]+1)*h[i]得出长度 动态转移方程while(temp>1 && h[temp-1]>=h[i]) temp=dpl[temp-1] /************************************************************************* > File Name: hdu1506.cpp > Author: yang &

Largest Rectangle in a Histogram

Largest Rectangle in a Histogram HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the hi

HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1506 Appoint description: Description A histogram is a polygon composed of a sequence of rectangles aligned a

HDU 1506 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 150664-bit integer IO format: %I64d      Java class name: Main A histogram is a polygon composed of a sequence of rectangles al

hdu 1506 Largest Rectangle in a Histogram 构造

题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rec

HDU 1506 &amp;&amp; POJ 2559 Largest Rectangle in a Histogram (单调队列)

题目链接:POJ 2559  Largest Rectangle in a Histogram 题目链接:HDU 1506  Largest Rectangle in a Histogram 题意:给出一串序列表示对应矩形的高度,求整个图中最大的矩形区域. 2, 1, 4, 5, 1, 3, 3 如图所示: 思路:每个矩形向左向右最大能扩张到的长度乘上他的高度,求最大值就是答案. 用单调队列维护序列递减,出队列的元素即是"极值"点 注意:要用int64. AC代码: #include&