51nod1102(单调栈/预处理)

题目链接: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 }
时间: 2024-10-16 09:15:18

51nod1102(单调栈/预处理)的相关文章

[51nod1102]面积最大的矩形(单调栈||预处理)

题意:求序列上某区间最小值乘区间长度的最大值. 解题关键:很早就在<挑战程序设计竞赛>中见过了,单调栈模板题,注意弹栈时如何处理后面的元素. 法一:单调栈 #include<bits/stdc++.h> using namespace std; typedef long long ll; stack<int>s; ll a[50002]; int main(){ int n; cin>>n; for(int i=0;i<n;i++) cin>&g

hdu 5875(单调栈)

Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1866    Accepted Submission(s): 674 Problem Description The shorter, the simpler. With this problem, you should be convinced of this tr

Code Forces Gym 100971D Laying Cables(单调栈)

D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. Whe

POJ2796---Feel Good(前缀和+单调栈维护)

Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life. A new idea Bill has recently developed assigns

Gym 100971D Laying Cables 单调栈

Description One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. When one-dimensional country got the Internet, it was decided to place the main server

【CF815D】Karen and Cards 单调栈+扫描线

[CF815D]Karen and Cards 题意:一张卡片有三个属性a,b,c,其上限分别为A,B,C,现在有n张卡片,定义一张卡片能打败另一张卡片当且仅当它的至少两项属性要严格大于另一张的对应属性.问在所有可能的卡片中,有多少种能打败这全部n张卡. n,A,B,C<=500000 题解:我们反过来,统计哪些卡片不能打败全部的卡. 我们先确定一个属性c,那么对于某张卡片(ai,bi,ci),如果c<=ci,则要求!(a>ai&&b>bi):如果c>ci,则

[51nod 1288]汽油补给(ST表+单调栈)

[51nod 1288]汽油补给(ST表+单调栈) 题面 有(N+1)个城市,0是起点N是终点,开车从0 -> 1 - > 2...... -> N,车每走1个单位距离消耗1个单位的汽油,油箱的容量是T.给出每个城市到下一个城市的距离D,以及当地的油价P,求走完整个旅途最少的花费.如果无法从起点到达终点输出-1. 分析 贪心考虑,当我们到达一个城市x的时候,我们下一个到的城市应该是在x加满油的情况下,能到达的油价比x低的城市.如果每个加油城市之间的路都这样走,那么最后的价钱一定是最小的.

【BZOJ-3956】Count ST表 + 单调栈

3956: Count Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 173  Solved: 99[Submit][Status][Discuss] Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N<=3*10^5,Ai<=10^9 Source CH Round#64 MFOI杯水题欢乐赛day1 By Gromah So

[Tyvj1939] 玉蟾宫(单调栈)

传送门 题目 Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地.这片土地被分成N*M个格子,每个格子里写着’R’或者’F’,R代表这块土地被赐予了rainbow,F代表这块土地被赐予了freda.现在freda要在这里卖萌...它要找一块矩形土地,要求这片土地都标着’F’并且面积最大.但是rainbow和freda的OI水平都弱爆了,找不出这块土地,而蓝兔也想看freda卖萌(她显然是不会编程的……)