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

39 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;
stack<int> stk; //栈
int a[N]; //高度数组
int b[N]; //能量数组
int s[N]; //每个发射塔的结果数组
int n; //发射塔个数
int res; //最大结果
/**
* 功能计算第i个发射塔它可以给哪个发射塔带来能量
* @param i
*/
void add(int i) {
//把栈顶高度比自己小的出栈这样的是不能享受i个发射塔带来的能量滴~
while (!stk.empty() && a[i] > a[stk.top()]) stk.pop();
//如果找着了一个高度比自己大的发射塔那么这个位置的发射塔将享受i个发射塔带来的能量。
if (!stk.empty()) s[stk.top()] += b[i];
//把自己入栈等待其它人给自己带来能量enjoy!
stk.push(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);
//清空栈
while (!stk.empty()) stk.pop();
//从右向左扫一遍
for (int i = n; i >= 1; i--) add(i);
//求最大值
for (int i = 1; i <= n; i++) res = max(s[i], res);
//输出
printf("%d", res);
}