poj 2559 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24218 Accepted: 7830

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 rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles: 

Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.

Input

The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1<=n<=100000. Then follow n integers h1,...,hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.

Output

For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.

Sample Input

7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0

Sample Output

8
4000

Hint

Huge input, scanf is recommended.

对于每一个H[i],都可以找到一个L[i]  R[i]使得L[i]=(j<=i并且h[j-1]<h[i]的最大的j)

R[i]=(j>i并且h[j]>h[i]的最小的j)

H=min(h[i]|L<=i<R)

h[L-1]肯定是小于H的

h[R]肯定也是小于H的

 1° 用数组模拟栈

#include <cstdio>
#include <stack>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
	{
		int n,L[100005],R[100005];
		int h[100005];
		int st[100005];
		while (scanf("%d",&n)!=EOF&&n!=0)
		{
			//memset(h,0,sizeof(h));
			//memset(L,0,sizeof(L));
			//memset(R,0,sizeof(R));
			for (int i=0;i<n;i++)
				scanf("%d",&h[i]);
			int t=0;
			for (int i=0;i<n;i++)
			{
				while (t>0&&h[st[t-1]]>=h[i])//st 存储的是j<=i并且h[j-1]<h[i]最大的j
					t--;
				L[i]=t==0?0:(st[t-1]+1);
				st[t++]=i;
			}
			t=0;
			for (int i=n-1;i>=0;i--)
			{
				while (t>0&&h[st[t-1]]>=h[i])
					t--;
				R[i]=t==0?n:(st[t-1]);
				st[t++]=i;
			}
			long long ans=0;
			for (int i=0;i<n;i++)
			ans=max(ans,(long long)h[i]*(R[i]-L[i]));
			printf("%lld\n",ans);
		}
	}

2° 使用栈解决

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
using namespace std;
int main()
    {
        int n,L[100005],R[100005];
        int h[100005];
        while (scanf("%d",&n)!=EOF&&n)
            {
                for (int i=0;i<n;i++)
                    scanf("%d",&h[i]);
                stack<int> s;
                //s.push(0);
                while (!s.empty()) s.pop();
                for (int i=0;i<n;i++)
                    {
                        while (!s.empty()&&h[s.top()]>=h[i])
                            s.pop();
                        if(s.empty()) L[i]=0;
                        else L[i]=s.top()+1;//因为s.top()是上一个位置,当前位置要加1
                        s.push(i);
                    }
                while (!s.empty()) s.pop();
                for (int i=n-1;i>=0;i--)
                    {
                        while (!s.empty()&&h[s.top()]>=h[i])
                            s.pop();
                        if(s.empty()) R[i]=n;
                        else R[i]=s.top();
                        s.push(i);
                    }
                long long ans=0;
                for (int i=0;i<n;i++)
                    ans=max(ans,(long long)h[i]*(R[i]-L[i]));
                    printf("%lld\n",ans);
            }
        return 0;
    }

提供了一个基于51单片机的RFID门禁系统的完整资源文件,包括PCB图、原理图、论文以及源程序。该系统设计由单片机、RFID-RC522频射卡模块、LCD显示、灯控电路、蜂鸣器报警电路、存储模块和按键组成。系统支持通过密码和刷卡两种方式进行门禁控制,灯亮表示开门成功,蜂鸣器响表示开门失败。 资源内容 PCB图:包含系统的PCB设计图,方便用户进行硬件电路的制作和调试。 原理图:详细展示了系统的电路连接和模块布局,帮助用户理解系统的工作原理。 论文:提供了系统的详细设计思路、实现方法以及测试结果,适合学习和研究使用。 源程序:包含系统的全部源代码,用户可以根据需要进行修改和优化。 系统功能 刷卡开门:用户可以通过刷RFID卡进行门禁控制,系统会自动识别卡片并判断是否允许开门。 密码开门:用户可以通过输入预设密码进行门禁控制,系统会验证密码的正确性。 状态显示:系统通过LCD显示屏显示当前状态,如刷卡成功、密码错误等。 灯光提示:灯亮表示开门成功,灯灭表示开门失败或未操作。 蜂鸣器报警:当刷卡或密码输入错误时,蜂鸣器会发出报警声,提示用户操作失败。 适用人群 电子工程、自动化等相关专业的学生和研究人员。 对单片机和RFID技术感兴趣的爱好者。 需要开发类似门禁系统的工程师和开发者。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值