• 单调栈(两边扩展)


    http://acm.hdu.edu.cn/showproblem.php?pid=1506

    题意:给出连续的一组宽度为1,高为a[i]的长方形 , 问整个图所能构成的面积最大的长方形。

    解法:单调递增栈记录以每一个值为最小值的两边扩展的最大值。比较以每一个值为最小值组成长方形面积的最大值。

    //#include<bits/stdc++.h>
    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    #include<cmath>
    using namespace std ;
    typedef long long ll ;
    const int N = 100010, M = 200010 ;
    int a[N] ;
    ll len[N];//以i为最小值,当前i所能向左向右扩展的最小值
    int st[N] , top ;
    int n ;
    void solve(){
        for(int i = 0 ; i < n ; i++){
            scanf("%d" , &a[i]);
            len[i] = 1 ;
        }
        top = 0 ; 
        int cnt = 0 ;
        for(int i = 0 ; i < n ; i++){
            cnt = 0 ;//当前右边扩展长度
            while(top && a[st[top]] >= a[i]){
                len[st[top]] += cnt ;//我被弹(扩展右边)
                cnt = len[st[top]] ;
                top-- ;
            }
            len[i] += cnt ;//我弹别人(扩展左边)
            st[++top] = i ;
        }
        cnt = 0 ;
        while(top){
            len[st[top]] += cnt ;
            cnt = len[st[top]] ;
            top-- ;
            
        }
        ll ans = 0 ; 
        for(int i = 0 ; i < n ; i++){
            ans = max(ans , len[i]*a[i]);
        }
        cout << ans << endl;
    }
    
    signed main(){
        #ifdef ONLINE_JUDGE
    	#else
    		freopen("D:\c++\in.txt", "r", stdin);
    		//freopen("D:\c++\out.txt", "w", stdout);
    	#endif
        while(~scanf("%d" , &n) && n)
            solve();
        
    }
    
  • 相关阅读:
    类图的基本知识
    设计模式——单例模式
    解决phpcms V9 推荐位无法排序
    PHPCMS 多站点管理切换问题
    天天团购系统-简单的目录结构
    php的时间输出格式
    天天团购--源码目录结构
    天天团购系统--部分模板语法
    PHP json数据格式化方法
    PHP json_encode中文乱码解决方法
  • 原文地址:https://www.cnblogs.com/nonames/p/12300035.html
Copyright © 2020-2023  润新知