「CF5E」Bindian Signalizing
传送门
Luogu
解题思路
很显然的一点,任何一条可能成为路径的圆弧都不可能经过最高的点,除非这条路径全是最高点。
所以我们先把最大值抠掉,把剩下的按原来的顺序排好。
从前往后、从后往前扫两次,用单调栈维护,记得计算连续的重复数字之后累加。
最后再用最大值随便匹配一下。
细节注意事项
- 咕咕咕。。。
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename 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;
}
typedef long long LL;
const int _ = 1000000 + 10;
int n, a[_], s[_];
int top, st[_], cnt[_], vis[_];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int i = 1; i <= n; ++i) read(a[i]), cnt[i] = 0, vis[i] = 0;
int mp = 0, len = 0;
for (rg int i = 1; i <= n; ++i) if (a[i] > a[mp]) mp = i;
for (rg int i = mp + 1; i <= n; ++i) s[++len] = a[i];
for (rg int i = 1; i <= mp - 1; ++i) s[++len] = a[i];
LL ans = 0;
top = 0;
for (rg int i = 1; i <= len; ++i) {
while (top && s[st[top]] <= s[i]) {
if (s[st[top]] == s[i]) cnt[i] = cnt[st[top]] + 1;
--top;
}
if (top) ++ans;
st[++top] = i;
}
top = 0;
for (rg int i = len; i >= 1; --i) {
while (top && s[st[top]] <= s[i]) --top;
if (top) ++ans;
st[++top] = i;
}
for (rg int i = 1; i <= len; ++i) ans += cnt[i];
int mid = 0;
for (rg int i = 1; i <= len; ++i)
if (s[i] >= mid) ans += !vis[i], vis[i] = 1, mid = s[i];
mid = 0;
for (rg int i = len; i >= 1; --i)
if (s[i] >= mid) ans += !vis[i], vis[i] = 1, mid = s[i];
printf("%lld\n", ans);
return 0;
}
完结撒花 \(qwq\)