結果

問題 No.956 Number of Unbalanced
ユーザー りあんりあん
提出日時 2019-12-20 00:26:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 1,717 bytes
コンパイル時間 2,594 ms
コンパイル使用メモリ 216,404 KB
実行使用メモリ 11,176 KB
最終ジャッジ日時 2023-09-21 07:51:17
合計ジャッジ時間 5,389 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,360 KB
testcase_01 AC 3 ms
5,332 KB
testcase_02 AC 4 ms
5,344 KB
testcase_03 AC 4 ms
5,288 KB
testcase_04 AC 4 ms
5,404 KB
testcase_05 AC 3 ms
5,328 KB
testcase_06 AC 34 ms
6,348 KB
testcase_07 AC 31 ms
6,216 KB
testcase_08 AC 32 ms
6,488 KB
testcase_09 AC 27 ms
6,212 KB
testcase_10 AC 26 ms
6,148 KB
testcase_11 AC 44 ms
6,880 KB
testcase_12 AC 33 ms
6,396 KB
testcase_13 AC 24 ms
11,096 KB
testcase_14 AC 52 ms
7,280 KB
testcase_15 AC 18 ms
6,080 KB
testcase_16 AC 35 ms
7,012 KB
testcase_17 AC 16 ms
6,272 KB
testcase_18 AC 48 ms
7,216 KB
testcase_19 AC 21 ms
7,488 KB
testcase_20 AC 48 ms
7,216 KB
testcase_21 AC 25 ms
11,176 KB
testcase_22 AC 16 ms
6,008 KB
testcase_23 AC 36 ms
7,012 KB
testcase_24 AC 21 ms
7,376 KB
testcase_25 AC 30 ms
6,268 KB
testcase_26 AC 26 ms
5,952 KB
testcase_27 AC 26 ms
6,108 KB
testcase_28 AC 32 ms
6,308 KB
testcase_29 AC 52 ms
7,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int M = 1000000007;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<vector<int>> vv(100010);
    for (int i = 0; i < n; ++i) {
        int a;
        cin >> a;
        vv[a].push_back(i + 1);
    }
    long long ans = 0;
    for (const auto& v : vv) {
        if (v.size() == 0) continue;
        stack<P> st, st2;
        st.push({0, 0});
        st2.push({0, 0});
        unordered_map<int, long long> mp;
        mp[0] = 0;
        int pd = 0, pj = 0;
        for (int j : v) {
            int d = pd - (j - pj - 1) + 1;
            if (pj != j - 1 && mp.count(d - 1)) {
                ans += mp[d - 1];
            }

            while (!st.empty() && st.top().first < d) {
                st.pop();
            }
            bool fir = true;
            while (!st2.empty() && st2.top().first >= d) {
                if (!fir) ans += mp[st2.top().first];
                fir = false;
                st2.pop();
            }
            if (!st.empty()) {
                int k = st.top().second + st.top().first - d;
                mp[d] += j - k - 1;
                ans += mp[d];
            }
            else {
                mp[d] = j;
                ans += j;
            }
            st.push({d, j});
            st2.push({d, j});
            pd = d;
            pj = j;
        }
        {
            bool fir = true;
            while (!st2.empty() && st2.top().first >= pd - n + pj) {
                if (!fir) ans += mp[st2.top().first];
                fir = false;
                st2.pop();
            }
        }
    }
    cout << ans << '\n';

    return 0;
}
0