「Luogu1901」发射站

传送门
Luogu

解题思路

单调栈裸题,扫两遍处理出每个点左边第一个比他高的和右边第一个比他高的,然后模拟题意即可。

细节注意事项

  • 咕咕咕。

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < class T > inline void read(T& s) {
	s = 0; int f = 0; char c = getchar();
	while (!isdigit(c)) f |= c == '-', c = getchar();
	while (isdigit(c)) s = s * 10 + c - 48, c = getchar();
	s = f ? -s : s;
}

const int _ = 1000002;

int n, H[_], V[_], top, stk[_];
long long val[_];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	read(n);
	for (rg int i = 1; i <= n; ++i) read(H[i]), read(V[i]);
	top = 0;
	for (rg int i = 1; i <= n; ++i) {
		while (top && H[stk[top]] <= H[i]) --top;
		val[stk[top]] += V[i], stk[++top] = i;
	}
	top = 0;
	for (rg int i = n; i >= 1; --i) {
		while (top && H[stk[top]] <= H[i]) --top;
		val[stk[top]] += V[i], stk[++top] = i;
	}
	long long ans = 0;
	for (rg int i = 1; i <= n; ++i)
		ans = max(ans, val[i]);
	printf("%lld\n", ans);
	return 0;
}

完结撒花 \(qwq\)

posted @ 2019-11-06 15:29  Sangber  阅读(118)  评论(0编辑  收藏  举报