CDOJ 1069 秋实大哥去打工 单调栈 下标处理

E - 秋实大哥去打工
Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu
Appoint description: 

Description

天行健,君子以自强不息。地势坤,君子以厚德载物。

天天过节的秋实大哥又要过节了,于是他要给心爱的妹子买礼物。但由于最近秋实大哥手头拮据,身为一个男人,他决定去打工!

秋实大哥来到一家广告公司。现在有n块矩形墙从左至右紧密排列,每一块高为Hi,宽为Wi

公司要求秋实大哥找出一块最大的连续矩形区域,使得公司可以在上面贴出最大的海报。

Input

第一行包含一个整数n,表示矩形墙的个数。

接下来n行,每行有两个整数WiHi,表示第i块墙的宽度和高度。

1n200000,保证WiHi以及最后的答案<231

Output

最大的连续矩形的面积。

Sample Input


3 4 
1 2 
3 4

Sample Output

14

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1);
const int maxn=200000;
int w[maxn+10],h[maxn+10];
int l[maxn+10],r[maxn+10],x[maxn+10];
 
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        MM(l,0);MM(r,0);MM(x,0);
        for(int i=1;i<=n;i++)
                scanf("%d %d",&w[i],&h[i]);
        x[1]=1;
        for(int i=2;i<=n+1;i++)
              x[i]=x[i-1]+w[i-1];//处理每个点的坐标
        stack<int> stl;
        for(int i=1;i<=n;i++)
        {
            while(stl.size()&&h[stl.top()]>=h[i]) stl.pop();
            l[i]=stl.size()==0?1:stl.top()+1;
            stl.push(i);
        }
 
        stack<int> str;
        for(int i=n;i>=1;i--)
        {
            while(str.size()&&h[i]<=h[str.top()]) str.pop();
            r[i]=str.size()==0?n+1:str.top();
            str.push(i);
        }
 
        int maxn=0;
        for(int i=1;i<=n;i++)
                maxn=max(maxn,(x[r[i]]-x[l[i]])*h[i]);
        printf("%d\n",maxn);
    }
    return 0;
}

  分析:裸的单调栈,需要处理好每个点的下标就好

posted @   快点说我帅  阅读(233)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
阅读排行:
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 2025成都.NET开发者Connect圆满结束
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 用一种新的分类方法梳理设计模式的脉络
点击右上角即可分享
微信分享提示