結果

問題 No.956 Number of Unbalanced
ユーザー りあんりあん
提出日時 2019-12-20 01:04:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,428 bytes
コンパイル時間 3,161 ms
コンパイル使用メモリ 215,616 KB
実行使用メモリ 10,560 KB
最終ジャッジ日時 2023-09-21 07:54:22
合計ジャッジ時間 4,331 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,436 KB
testcase_01 AC 2 ms
5,348 KB
testcase_02 AC 4 ms
5,372 KB
testcase_03 AC 3 ms
5,528 KB
testcase_04 AC 3 ms
5,348 KB
testcase_05 AC 3 ms
5,416 KB
testcase_06 AC 29 ms
6,400 KB
testcase_07 AC 30 ms
6,144 KB
testcase_08 AC 28 ms
6,428 KB
testcase_09 AC 22 ms
6,140 KB
testcase_10 AC 26 ms
5,928 KB
testcase_11 AC 38 ms
7,004 KB
testcase_12 AC 29 ms
6,488 KB
testcase_13 AC 24 ms
10,560 KB
testcase_14 AC 41 ms
7,200 KB
testcase_15 AC 16 ms
5,956 KB
testcase_16 AC 32 ms
7,080 KB
testcase_17 AC 16 ms
6,328 KB
testcase_18 AC 42 ms
7,252 KB
testcase_19 AC 21 ms
7,512 KB
testcase_20 AC 43 ms
7,244 KB
testcase_21 AC 24 ms
10,500 KB
testcase_22 AC 17 ms
6,140 KB
testcase_23 AC 31 ms
6,932 KB
testcase_24 AC 20 ms
7,444 KB
testcase_25 AC 30 ms
6,140 KB
testcase_26 AC 22 ms
5,948 KB
testcase_27 AC 26 ms
6,136 KB
testcase_28 AC 31 ms
6,300 KB
testcase_29 AC 44 ms
7,260 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;
        st.push({0, 0});
        unordered_map<int, long long> mp;
        mp[0] = 0;
        int pd = 0, pj = 0;
        int mind = 0;
        for (int j : v) {
            int d = pd - (j - pj - 1) + 1;
            mind = min(mind, d);
            if (pj != j - 1) {
                ans -= mp[pd];
                if (mp.count(d - 1))
                    ans += mp[d - 1];
            }

            while (!st.empty() && st.top().first < d) {
                st.pop();
            }
            if (!st.empty()) {
                int k = st.top().second + st.top().first - d;
                mp[d] += j - k - 1;
                ans += mp[d] * 2;
            }
            else {
                mp[d] = j;
                ans += j * 2;
            }
            st.push({d, j});
            pd = d;
            pj = j;
        }
        ans -= mp[pd];
        for (int i = mind; i < pd - n + pj; ++i) {
            ans -= mp[i];
        }
    }
    cout << ans << '\n';

    return 0;
}
0