码迷,mamicode.com
首页 > 其他好文 > 详细

51nod1102(单调栈/预处理)

时间:2017-01-24 02:14:32      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:题意   思路   for   com   预处理   size   单调栈   blank   queue   

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102

 

题意:中文题诶~

 

思路:单调栈/预处理 (这篇博客就不细写了啦,只给出代码和转过来的两篇不错的题解,好困了~)

 

单调栈:http://blog.csdn.net/u012773338/article/details/40265223

代码:

#include <iostream>
#include <stack>
#define ll long long
#define MAXN 50010
using namespace std;

ll a[MAXN], ans=0;
stack<int> st;

int main(void){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> a[i];
    }
    a[n]=-1;
    for(int i=0; i<=n; i++){
        if(st.empty()||a[i]>=a[st.top()]){
            st.push(i);
        }else if(a[i]<a[st.top()]){
            int cnt;
            while(!st.empty()&&a[i]<a[st.top()]){
                ans=max(ans, a[st.top()]*(i-st.top()));
                cnt=st.top();
                st.pop();
            }
            st.push(cnt);
            a[cnt]=a[i];
        }
    }
    cout << ans << endl;
    return 0;
}

 

预处理:http://blog.csdn.net/queuelovestack/article/details/52326276

代码:

 1 #include <iostream>
 2 #define ll long long
 3 #define MAXN 50010
 4 using namespace std;
 5 
 6 ll l[MAXN], r[MAXN], a[MAXN];
 7 
 8 int main(void){
 9     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
10     int n;
11     cin >> n;
12     for(int i=1; i<=n; i++){
13         cin >> a[i];
14     }
15     for(int i=1; i<=n; i++){
16         int k=i-1;
17         while(a[i]<=a[k]){
18             k=l[k]-1;
19         }
20         l[i]=k+1;
21     }
22     for(int i=n; i>0; i--){
23         int k=i+1;
24         while(a[i]<=a[k]){
25             k=r[k]+1;
26         }
27         r[i]=k-1;
28     }
29     ll ans=0;
30     for(int i=1; i<=n; i++){
31         ans=max((r[i]-l[i]+1)*a[i], ans);
32     }
33     cout << ans << endl;
34 }

 

51nod1102(单调栈/预处理)

标签:题意   思路   for   com   预处理   size   单调栈   blank   queue   

原文地址:http://www.cnblogs.com/geloutingyu/p/6345540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!