結果

問題 No.956 Number of Unbalanced
ユーザー りあんりあん
提出日時 2019-12-20 01:12:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,153 bytes
コンパイル時間 2,325 ms
コンパイル使用メモリ 215,040 KB
実行使用メモリ 10,588 KB
最終ジャッジ日時 2023-09-21 07:54:40
合計ジャッジ時間 4,284 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,280 KB
testcase_01 AC 4 ms
5,568 KB
testcase_02 AC 3 ms
5,300 KB
testcase_03 AC 3 ms
5,280 KB
testcase_04 AC 4 ms
5,292 KB
testcase_05 AC 4 ms
5,572 KB
testcase_06 AC 30 ms
6,400 KB
testcase_07 AC 31 ms
6,156 KB
testcase_08 AC 28 ms
6,448 KB
testcase_09 AC 23 ms
6,220 KB
testcase_10 AC 26 ms
5,952 KB
testcase_11 AC 40 ms
6,876 KB
testcase_12 AC 31 ms
6,608 KB
testcase_13 AC 25 ms
10,588 KB
testcase_14 AC 42 ms
7,152 KB
testcase_15 AC 15 ms
6,224 KB
testcase_16 AC 32 ms
6,936 KB
testcase_17 AC 16 ms
6,256 KB
testcase_18 AC 42 ms
7,508 KB
testcase_19 AC 21 ms
7,432 KB
testcase_20 AC 45 ms
7,196 KB
testcase_21 AC 26 ms
10,480 KB
testcase_22 AC 16 ms
6,168 KB
testcase_23 AC 32 ms
6,964 KB
testcase_24 AC 21 ms
7,480 KB
testcase_25 AC 30 ms
6,240 KB
testcase_26 AC 23 ms
5,872 KB
testcase_27 AC 26 ms
6,196 KB
testcase_28 AC 30 ms
6,412 KB
testcase_29 AC 45 ms
7,192 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;
        int pd = 0, pj = 0, md = 0;
        for (int j : v) {
            int d = pd - j + pj + 2;
            md = min(md, 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();

            mp[d] += st.empty() ? j : j - st.top().second - st.top().first + d - 1;
            ans += mp[d] * 2;
            st.push({d, j});
            pd = d;
            pj = j;
        }
        ans -= mp[pd];
        for (int i = md; i < pd - n + pj; ++i) ans -= mp[i];
    }
    cout << ans << '\n';
    return 0;
}
0