[dp]POJ2559 && HDOJ1506 Largest Rectangle in a Histogram

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   sp   for   

題意 給n個條形的高度, 問能放的最大矩形面積

分析: 從左至右 從右至左 各搞一遍

分別記錄      L[i]記錄列(從前往後)標 第幾列開始 可以往後放高度為a[i]的矩形

       R[i]記錄列(從後往前)標 第幾列開始 可以往前放高度為a[i]的矩形

 R[i]-L[i]+1即為高度為a[i]的矩形能橫穿的列數

再乘個a[i]即為面積  遍曆所有高度 求最大值即可

 

ps.注意範圍 面積要用LL

pps.注意 max((int), (LL)) 的CE

 

 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <climits> 5 #include <cctype> 6 #include <cmath> 7 #include <string> 8 #include <sstream> 9 #include <iostream>10 #include <algorithm>11 #include <iomanip>12 using namespace std;13 #include <queue>14 #include <stack>15 #include <vector>16 #include <deque>17 #include <set>18 #include <map>19 typedef long long LL;20 typedef long double LD;21 #define pi acos(-1.0)22 #define lson l, m, rt<<123 #define rson m+1, r, rt<<1|124 typedef pair<int, int> PI;25 typedef pair<int, PI> PP;26 #ifdef _WIN3227 #define LLD "%I64d"28 #else29 #define LLD "%lld"30 #endif31 //#pragma comment(linker, "/STACK:1024000000,1024000000")32 //LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}33 //inline int read(){char ch=‘ ‘;int ans=0;while(ch<‘0‘ || ch>‘9‘)ch=getchar();while(ch<=‘9‘ && ch>=‘0‘){ans=ans*10+ch-‘0‘;ch=getchar();}return ans;}34 inline void print(LL x){printf(LLD, x);puts("");}35 //inline void read(double &x){char c = getchar();while(c < ‘0‘) c = getchar();x = c - ‘0‘; c = getchar();while(c >= ‘0‘){x = x * 10 + (c - ‘0‘); c = getchar();}}36 37 #define N 10000538 int a[N], L[N], R[N];39 int main()40 {41 #ifndef ONLINE_JUDGE42     freopen("in.txt", "r", stdin);43     freopen("out.txt", "w", stdout);44 #endif45     int n;46     while(~scanf("%d", &n) && n)47     {48         for(int i=0;i<n;i++)49             scanf("%d", &a[i]);50         memset(L, 0, sizeof(L));51         memset(R, 0, sizeof(R));52         for(int i=0;i<n;i++)53         {54             L[i]=i;55             while(L[i]>0 && a[L[i]-1]>=a[i])56                 L[i]=L[L[i]-1];57         }58         for(int i=n-1;i>=0;i--)59         {60             R[i]=i;61             while(R[i]<n-1 && a[R[i]+1]>=a[i])62                 R[i]=R[R[i]+1];63         }64         LL ans=0;65         for(int i=0;i<n;i++)66             ans=max(ans, (LL)(R[i]-L[i]+1)*a[i]);67         print(ans);68     }69     return 0;70 }
HDOJ1506

 

[dp]POJ2559 && HDOJ1506 Largest Rectangle in a Histogram

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.