[题解]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;
}

浙公网安备 33010602011771号