結果

問題 No.956 Number of Unbalanced
ユーザー nebukuro09nebukuro09
提出日時 2019-12-19 01:44:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,923 bytes
コンパイル時間 1,632 ms
コンパイル使用メモリ 168,692 KB
実行使用メモリ 10,472 KB
最終ジャッジ日時 2023-09-21 04:41:51
合計ジャッジ時間 6,422 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
10,472 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 213 ms
4,708 KB
testcase_07 AC 208 ms
4,388 KB
testcase_08 AC 212 ms
4,956 KB
testcase_09 AC 206 ms
4,380 KB
testcase_10 AC 203 ms
4,376 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

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];
int 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[A[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;
        int counter = 0; // pointer 未満の合計
        D[OFFSET] += 1;
        REP(i, N) {
            if (A[i] == a) {
                pointer += 1;
                counter += D[pointer - 1];
            } else {
                pointer -= 1;
                counter += D[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