2020-2021 “Orz Panda” Cup A. Accordion Artist And Orz Pandas(单调栈)

Posted li_wen_zhuo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020-2021 “Orz Panda” Cup A. Accordion Artist And Orz Pandas(单调栈)相关的知识,希望对你有一定的参考价值。

题目描述

Chuan the Country Builder is a famous accordion artist. Now he wants to put a “Make Accordion Great Again” advertisment, in order to get some advantage over his enemy Bai the Sleeper.
There are n buildings on the 114514th Avenue, the Old York city. The premium alongside the 114514th Avenue is very high so there is no margin between two adjacent buildings. The front view of the i-th building can be approximated with a rectangle with height hi and width wi.
Chuan wants his advertisment to be a great one. More specifically, it must be a rectangle with the height perpendicular to the height of the front view of each building, and the entire advertisment must fit in the union of the front views of the buildings.
An Orz Panda company has made a contract to build the advertisment for Chuan. The company wants the area of the advertisment to be as large as possible, so they can get more money from Chuan. Please calculate the maximum possible area of all great advertisments.

Input

There is only one test case in the input file.
The first line contains an integer n, the number of the buildings.
The i-th of the following n lines contains two integers hi, wi, the height and weight of the i-th building.
It’s guaranteed that 1≤n≤105, and 1≤hi,wi≤105.

Output

Output one line containing the maximum area.

Examples

input
5
2 1
4 1
3 1
4 1
1 1
output
9
input
5
2 1
4 1
3 1
4 1
2 1
output
10

Note

The great advertisment with maximum area is illustrated in the following figure. The grey area is the front view of the buildings. There is some margin deliberately added around the advertisment, to make the figure clear.
在这里插入图片描述

题目大意

有n栋楼,第i栋楼的高为h[i],宽为w[i]。有人想在这些楼上挂一块长方形的广告牌,问这块广告牌的最大面积是多少。

题目分析

单调栈的模板题我居然花了这么长时间才看出来。。。。

我们可以提前预处理出:l[i] //第i栋楼左边第一栋比它矮的楼的编号 和 r[i] //第i栋楼右边第一栋比它矮的楼的编号 这两个数组(预处理可以用单调栈来完成)。

这样就可以快速求出高度为h[i]的广告牌所占的区间大小了(为了使广告牌的面积最大,广告牌的高度一定是h[]中的一个值)。然后我们可以对w[]进行前缀和处理,这样就可以O(1)求出高度为h[i]的广告牌的面积了。

最后我们枚举每栋楼,记录高度为h[i]的广告牌中的最大值即可。

代码如下
#include <iostream>
#include <cmath>
#include <cstdio>
#include <set>
#include <string>
#include <cstring>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#define LL long long
#define ULL unsigned long long
#define PII pair<int,int>
#define PDD pair<double,double>
#define x first
#define y second
using namespace std;
const int N=2e6+5,INF=1e9+7;
int h[N],stk[N],top;
LL w[N];
int l[N],r[N];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&h[i],&w[i]);
        w[i]+=w[i-1];					//对w[]进行前缀和处理
    }
    h[0]=h[n+1]=0;						//设置边界
    for(int i=1;i<=n+1;i++)				//单调栈预处理l[]和r[]
    {
        while(h[stk[top]]>h[i]) r[stk[top]]=i,top--;
        l[i]=stk[top];
        stk[++top]=i;
    }
    LL ans=0;
    for(int i=1;i<=n;i++)			//求出高度为h[i]的广告牌的面积,记录最大值
        ans=max(ans,(w[r[i]-1]-w[l[i]])*h[i]);
    printf("%lld\\n",ans);
    return 0;
}

以上是关于2020-2021 “Orz Panda” Cup A. Accordion Artist And Orz Pandas(单调栈)的主要内容,如果未能解决你的问题,请参考以下文章

集合运算基本法则

linux 邮件报警,监控内存cup

2015-2016 XVI Open Cup, Grand Prix of Bashkortostan, SKB Kontur Cup Stage 2

HDU 2289 Cup

P4336 [SHOI2016]黑暗前的幻想乡

简单的全网备份脚本样板