题解 | 能量辐射

能量辐射

https://www.nowcoder.com/practice/04c83aedd3e043879ed4464baae0b564

很裸的一个单调栈模板,代码里面的两个for循环分别是找到左边和右边第一个比i大的位置,然后我们直接给这个位置累加一下a_i的能量就行了。

#include <bits/stdc++.h>
#define il inline

using namespace std;
using ll = long long;
using ull = unsigned long long;
using int128=__int128_t;

const ll N = 1e5 + 5, mod =998244353, inf = 2e18;
const double esp=1e-9;
double PI=3.1415926;

il void solve(){
    ll n;
    cin>>n;
    vector<pair<ll,ll>>a(n+1);
    for(int i=1;i<=n;i++){
        cin>>a[i].first>>a[i].second;
    }
    vector<ll>ans(n+5),stak(n+5);
    int top=0;
    for(int i=1;i<=n;i++){
        while(top&&a[stak[top]].first<=a[i].first)top--;
        ans[stak[top]]+=a[i].second;
        stak[++top]=i;
    }
    top=0;
    for(int i=n;i>=1;i--){
        while(top&&a[stak[top]].first<=a[i].first)top--;
        ans[stak[top]]+=a[i].second;
        stak[++top]=i;
    }
    cout<<*max_element(ans.begin()+1,ans.end());
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    
    int t = 1;

    // cin >> t;

    while (t--) {

        solve();

    }

    return 0;
}

全部评论

相关推荐

10-28 10:48
已编辑
门头沟学院 Java
孩子我想要offer:发笔试后还没笔试把我挂了,然后邮箱一直让我测评没测,后面不知道干嘛又给我捞起来下轮笔试,做完测评笔试又挂了😅
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务