P1901 发射站(单调栈)

P1901 发射站

解题思路:单调维护递减。找到某一个数大于栈顶元素时,这个为止就可以加上 栈顶的能量值。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lf;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int inf = 0x7f7f7f7f;
const ll INF = 1e16;
const int N = 1e6+10;
const ull base = 131;
const ll mod =  1e9+7;


inline int 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;}
inline string readstring(){string str;char s=getchar();while(s==' '||s=='\n'||s=='\r'){s=getchar();}while(s!=' '&&s!='\n'&&s!='\r'){str+=s;s=getchar();}return str;}
int random(int n){return (int)(rand()*rand())%n;}
void writestring(string s){int n = s.size();for(int i = 0;i < n;i++){printf("%c",s[i]);}}
bool is_prime(int n){if(n <2) return false;for(int i = 2;i*i <= n;i++){if(n%i == 0) return false;}return true;}


int h[N],v[N];
ll num[N];

int main(){
    //freopen("in.txt","r",stdin);
    srand((unsigned)time(NULL));
    int n = read();
    for(int i = 1;i <= n;i++){
        h[i] = read();
        v[i] = read();
    }
    stack<int>st;
    for(int i = 1;i <= n;i++){
        while(st.size()&&h[st.top()]<h[i]){
            num[i] += v[st.top()];
            st.pop();
        }
        st.push(i);
    }
    while(st.size()) st.pop();
    for(int i = n;i >= 1;i--){
        while(st.size()&&h[st.top()]<h[i]){
            num[i] += v[st.top()];
            st.pop();
        }
        st.push(i);
    }
    ll ans = 0;
    for(int i = 1;i <= n;i++){
        //printf("%d ",num[i]);
        ans = max(ans,num[i]);
    }
    cout<<ans<<endl;
    return 0;
}
/*
7
2 4 1 2 2 5 1
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值