2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈
生活随笔
收集整理的這篇文章主要介紹了
2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
秋實(shí)大哥去打工
Time Limit: 1 Sec??Memory Limit: 256 MB
題目連接
http://acm.uestc.edu.cn/#/contest/show/59Description
天行健,君子以自強(qiáng)不息。地勢(shì)坤,君子以厚德載物。
天天過(guò)節(jié)的秋實(shí)大哥又要過(guò)節(jié)了,于是他要給心愛(ài)的妹子買(mǎi)禮物。但由于最近秋實(shí)大哥手頭拮據(jù),身為一個(gè)男人,他決定去打工!
秋實(shí)大哥來(lái)到一家廣告公司。現(xiàn)在有n塊矩形墻從左至右緊密排列,每一塊高為Hi,寬為Wi。
公司要求秋實(shí)大哥找出一塊最大的連續(xù)矩形區(qū)域,使得公司可以在上面貼出最大的海報(bào)。
Input
第一行包含一個(gè)整數(shù)n,表示矩形墻的個(gè)數(shù)。接下來(lái)n行,每行有兩個(gè)整數(shù)Wi,Hi,表示第i塊墻的寬度和高度。
1≤n≤200000,保證Wi,Hi以及最后的答案<231。
Output
最大的連續(xù)矩形的面積。Sample Input
33 4
1 2
3 4
Sample Output
14HINT
題意
題解:
代碼:
?
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 200001 #define mod 10007 #define eps 1e-9 //const int inf=0x7fffffff; //無(wú)限大 const int inf=0x3f3f3f3f; /* inline ll read() {int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f; } int buf[10]; inline void write(int i) {int p = 0;if(i == 0) p++;else while(i) {buf[p++] = i % 10;i /= 10;}for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);printf("\n"); } */ //************************************************************************************** using namespace std; long long a[maxn],b[maxn],ans; int r[maxn],l[maxn]; stack<int> s; int main() {int n;scanf("%d",&n);a[0]=a[n+1]=-1;for(int i=1;i<=n;i++){scanf("%d",&b[i]);b[i]+=b[i-1];scanf("%lld",&a[i]);}s.push(0);int p=0;for(int i=1;i<=n;i++){for(p=s.top();a[p]>=a[i];p=s.top())s.pop();l[i]=p+1;s.push(i);}while(!s.empty())s.pop();s.push(n+1);for(int i=n;i>0;i--){for(p=s.top();a[p]>=a[i];p=s.top())s.pop();r[i]=p-1;s.push(i);}for(int i=1;i<=n;i++)ans=max(ans,((b[r[i]]-b[i-1])+(b[i-1]-b[l[i]-1]))*a[i]);printf("%lld\n",ans);return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/qscqesze/p/4427282.html
總結(jié)
以上是生活随笔為你收集整理的2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Core Java Volume I —
- 下一篇: Vim简本