[题解]P1901 发射站

单调栈水题,强烈建议本题降黄

#include<bits/stdc++.h>

using namespace std;
using ll=long long;
using tiiii=tuple<int,int,int,int,int>;//height index data ans
vector<tiiii>v;
stack<tiiii>s1,s2;
int n;
bool cmp(tiiii a,tiiii b){
    return get<3>(a)<get<3>(b);
}
void nge(){
    for(int i=0;i<v.size();i++){
        if(s1.empty()||get<0>(s1.top())>=get<0>(v[i])) s1.push(v[i]);
        else{
            while(s1.size()&&get<0>(s1.top())<get<0>(v[i])){
                get<3>(v[i])+=get<2>(v[get<1>(s1.top())]);
                s1.pop();
            }
            s1.push(v[i]);
        }
    }
}
void pge(){
    for(int i=0;i<v.size();i++){
        if(s2.empty()) s2.push(v[i]);
        else{
            while(s2.size()&&get<0>(s2.top())<=get<0>(v[i])) s2.pop();
            if(s2.size()) get<3>(v[get<1>(s2.top())])+=get<2>(v[i]);
            s2.push(v[i]);
        }
    }
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    v.resize(n);
    for(int i=0;i<n;i++){
        cin>>get<0>(v[i])>>get<2>(v[i]);
        get<1>(v[i])=i;
    }
    nge();
    pge();
    cout<<get<3>(*max_element(v.begin(),v.end(),cmp))<<endl;
    return 0;
}
posted @   椰萝Yerosius  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示