zoukankan      html  css  js  c++  java
  • POJ2559 Largest Rectangle in a Histogram

    这道题一看我们可以发现一个木桶原理,只要有一个短的,长的就没用了,我们就可以维护一个单调栈,如果碰到比前面小的,就直接进行统计和修改,最后再设n+1个矩形高度为0来统计,注意清零

    #include<bits/stdc++.h>
    using namespace std;
    int st[100005],w[100005],s[100005],top,a[100005],n;
    long long ans;
    int main(){
        while(scanf("%d",&n)==1&&n){
            int k;
            for(int i=1;i<=n;i++)
             scanf("%d",&a[i]);
            top=a[n+1]=0;//巧设
            ans=0;
            for(int i=1;i<=n+1;i++){
                if(a[i]>s[top])
                 w[++top]=1,s[top]=a[i];//单调性
                else{
                 int wid=0;
                 while(s[top]>a[i]){
                     wid+=w[top];
                     ans=max(ans,(long long)wid*s[top]);//统计
                     top--; 
                 }
                 s[++top]=a[i];w[top]=wid+1;
                }
            }
            cout<<ans<<endl;
        }
    }
  • 相关阅读:
    jmeter参数化关联
    电商
    mysql联查
    购物车
    冒泡排序、二分查找、选择排序、斐波那契
    python数据转换/9*9表/for循环
    python三角形
    mysql语句
    Selenium 8
    Selenium 7
  • 原文地址:https://www.cnblogs.com/coder-cjh/p/11370462.html
Copyright © 2011-2022 走看看