按照 CF5E 的做法 g 了一大半 不是很理解为什么多数了
查看原帖
按照 CF5E 的做法 g 了一大半 不是很理解为什么多数了
411103
Patricky楼主2022/8/19 06:59

如题。注释的部分是 CF5E 的做法。

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define R ranges
#define RI R::iota_view

int main() {
    ios::sync_with_stdio(!cin.tie(0));

    int n;
    cin >> n;

    vector a(n, 0);

    for (int i : RI(0, n)) {
        cin >> a[i];
    }

    // int    top{};
    // vector stk(n + 1, 0);
    // vector cnt(n + 1, 0);
    //
    // ll ans{};
    // int m{};
    //
    // for (int i : RI(0, n)) {
    //     while (top && stk[top] < a[i]) {
    //         ans += cnt[top--];
    //     }
    //     ans += !!top;
    //     if (stk[top] != a[i]) {
    //         stk[++top] = a[i];
    //         cnt[top]   = 0;
    //     }
    //     ans += cnt[top]++;
    // }

    ll     ans{};
    int    top{};
    vector stk(n + 1, array<int, 2>{});

    for (int i : RI(0, n)) {
        int cnt{};
        while (top && stk[top][0] <= a[i]) {
            if (stk[top][0] == a[i]) {
                cnt = stk[top][1];
            }
            ans += stk[top--][1];
        }
        ans += !!top;
        stk[++top] = {a[i], cnt + 1};
    }

    cout << ans << " \n";

    return 0;
}
2022/8/19 06:59
加载中...