結果

問題 No.956 Number of Unbalanced
ユーザー nebukuro09nebukuro09
提出日時 2019-12-19 02:09:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 680 ms / 2,000 ms
コード長 1,918 bytes
コンパイル時間 1,529 ms
コンパイル使用メモリ 168,092 KB
実行使用メモリ 6,976 KB
最終ジャッジ日時 2023-09-21 04:44:56
合計ジャッジ時間 7,977 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 166 ms
4,748 KB
testcase_07 AC 155 ms
4,420 KB
testcase_08 AC 168 ms
4,684 KB
testcase_09 AC 165 ms
4,384 KB
testcase_10 AC 142 ms
4,376 KB
testcase_11 AC 680 ms
6,976 KB
testcase_12 AC 170 ms
4,900 KB
testcase_13 AC 82 ms
5,712 KB
testcase_14 AC 218 ms
5,080 KB
testcase_15 AC 82 ms
5,972 KB
testcase_16 AC 164 ms
4,720 KB
testcase_17 AC 82 ms
5,684 KB
testcase_18 AC 218 ms
5,124 KB
testcase_19 AC 82 ms
5,776 KB
testcase_20 AC 217 ms
5,108 KB
testcase_21 AC 83 ms
6,476 KB
testcase_22 AC 82 ms
5,732 KB
testcase_23 AC 164 ms
4,696 KB
testcase_24 AC 83 ms
5,848 KB
testcase_25 AC 668 ms
6,124 KB
testcase_26 AC 192 ms
4,380 KB
testcase_27 AC 140 ms
6,112 KB
testcase_28 AC 666 ms
6,128 KB
testcase_29 AC 216 ms
5,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);i++)
#define REP2(i,m,n) for (int i=m;i<(n);i++)
typedef long long ll;

const int SQ = 320;
const int OFFSET = 101010;

int N;
int A[101010];
int B[101010];
int C[101010];
ll D[202020];
int E[101010];
int cnt[101010];

void solve() {
    cin >> N;
    REP(i, N) cin >> A[i];
    REP(i, N) cnt[A[i]] += 1;

    vector<int> less, much;
    REP(i, 101010) if (cnt[i] > 0) (cnt[i] < SQ ? less : much).push_back(i);
    REP(i, N) (cnt[A[i]] < SQ ? B[i] : C[i]) = A[i];

    int L = less.size();
    int M = much.size();
    ll ans = 0;

    REP2(len, 1, min(N+1, SQ*2)) {
        for (auto a : less) {
            E[a] = 0;
        }
        bool ok = false;
        const int half = len / 2 + 1;
        REP(i, len) if (B[i] != 0) {
            E[B[i]] += 1;
            if (E[B[i]] == half) ok = true;
        }
        ans += ok;
        REP2(i, 1, N-len+1) {
            if (i > 0 && B[i-1] != 0) {
                if (E[B[i-1]] == half) ok = false;
                E[B[i-1]] -= 1;
            }
            if (B[i+len-1] != 0) {
                E[B[i+len-1]] += 1;
                if (E[B[i+len-1]] == half) ok = true;
            }
            ans += ok;
        }
    }

    for (const auto a : much) {
        memset(D, 0, sizeof(D));
        int pointer = OFFSET;
        ll counter = 0; // pointer 未満の合計
        D[OFFSET] += 1;
        REP(i, N) {
            if (A[i] == a) {
                pointer += 1;
                counter += D[pointer - 1];
            } else {
                counter -= D[pointer - 1];
                pointer -= 1;
            }
            D[pointer] += 1;
            ans += counter;
        }
        //cout << pointer - OFFSET << " " << counter << " " << D[pointer] << endl;
    }

    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
}
0