Files
python/TangDou/LuoGuBook/P2866.cpp

39 lines
955 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#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;
}