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

39 lines
955 B
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;
typedef long long LL;
const int N = 80010;
stack<int> stk;
LL a[N], res[N];
int n;
LL ans;
/**
输入样例
6
10 3 7 4 12 2
找出右侧第一个比自己大的数字
原值10 3 7 4 12 2
原号1 2 3 4 5 6
对值12 7 12 12 -1 -1
对号5 3 5 5 -1 -1
*/
int main() {
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
for (int i = 1; i <= n; i++) {
while (!stk.empty() && a[stk.top()] <= a[i]) {
res[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
for (int i = 1; i <= n; i++)
if (res[i]) ans += res[i] - i - 1;//两个号之间夹的需要j-i-1,比如4和2之间其实就是一个3就是4-2-1=1
else ans += n - i; //如果右侧没有比自己大的,那么就是一直到最右端,它都能看到。
//输出结果
cout << ans << endl;
return 0;
}