Files
python/TangDou/LuoGuBook/P1901.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

42 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int n;
int a[N], b[N]; //高度与能力值
int s[N];//结果数组
int ans; //最大值
//用数组模拟栈,每一个栈内的元素,都是需要找到“比自己高度小的,离自己最近”的发射塔,才能享受它带来的能量值。
int stk[N]; //内容:序号
int tt; //指针,默认是0
void add(int i) {
//把栈顶高度比自己小的出栈这样的是不能享受i个发射塔带来的能量滴~
while (tt && a[i] > a[stk[tt]]) tt--;
//如果找着了一个高度比自己大的发射塔那么这个位置的发射塔将享受i个发射塔带来的能量。
s[stk[tt]] += b[i];
//把自己入栈等待其它人给自己带来能量enjoy!
stk[++tt] = i;
}
int main() {
//输入
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
//从左向右扫一遍
for (int i = 1; i <= n; i++) add(i);
//还原指针,清空栈
tt = 0;
//从右向左扫一遍
for (int i = n; i >= 1; i--) add(i);
//找出结果
for (int i = 1; i <= n; i++) ans = max(ans, s[i]);
//输出
printf("%d", ans);
return 0;
}