zoukankan      html  css  js  c++  java
  • poj 2559 单调栈 ***

    给出一系列的1*h的矩形,求矩形的最大面积。
    如图:

    题解链接:点我

     1 #include<cstdio>
     2 #include<cmath>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<iostream>
     6 using namespace std;
     7 const int MAXN=100005;
     8 pair<int,int> ele[MAXN];
     9 int main(){
    10     int height, n;
    11     int w;
    12     int i,j,k;
    13     long long ans, tot, tmp;
    14     int top=0;
    15     #ifndef ONLINE_JUDGE
    16     freopen("1.txt","r",stdin);
    17     #endif
    18     while (scanf("%d", &n) != EOF && n)
    19     {
    20         top = 0;
    21         ans = 0;
    22         for(i=0;i<n;i++){
    23             tmp=0;
    24             scanf("%d",&w);
    25             while(top>0&&w<ele[top-1].first){
    26                 long long aans=(long long)ele[top-1].first*(ele[top-1].second+tmp);
    27                 ans=max(ans,aans);
    28                 tmp+=ele[top-1].second; //这个比较容易忘,这是当前累计的加上之前累计的
    29                 top--;
    30             }
    31             ele[top].first=w;
    32             ele[top].second=1+tmp;
    33             top++;
    34         }
    35         tmp=0;
    36         while(top>0){
    37                 long long aans=ele[top-1].first*(ele[top-1].second+tmp);
    38                 ans=max(ans,aans);
    39                 tmp+=ele[top-1].second;
    40                 top--;
    41         }
    42         printf("%lld
    ", ans);
    43 
    44 
    45     }
    46     return 0;
    47 
    48 }
  • 相关阅读:
    bootstrap初识
    司徒正美居中方式
    css中的浮动(float)
    fio
    简易接口测试脚本
    字符加密
    Python 汉诺塔
    Python 找零问题
    Python 二分法
    Python 冒泡排序
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/4670907.html
Copyright © 2011-2022 走看看