求助
查看原帖
求助
448474
ximena楼主2021/7/29 23:30

求助 感觉自己写的没有啥问题

#include <bits/stdc++.h>
using namespace std;
#define bug(x) cout << #x << " == " << x << endl;
typedef pair<int, int> ppp;
#define ll long long
const int INF = 0x3f3f3f3f;
const int MAX_N = 5e5+10;
const int maxn = 3e6+10;
int mod = 1e9 + 7;
ll a[maxn] = { 0 };
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    stack<int> s;
    ll sum = -1;
    for(int i=1;i<=n;i++)
    {
        if( (!s.empty())&&a[s.top()]<=a[i])
        {
            sum += (s.size()-1);
            while( (!s.empty())&&a[s.top()]<a[i] )
            {
                s.pop();
            }
        }
        sum++;
        s.push(i);
    }
    printf("%lld\n",sum);
}

2021/7/29 23:30
加载中...