結果

問題 No.1435 Mmm......
ユーザー ktr216ktr216
提出日時 2021-03-19 23:43:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 1,231 bytes
コンパイル時間 1,923 ms
コンパイル使用メモリ 169,832 KB
実行使用メモリ 22,592 KB
最終ジャッジ日時 2023-08-12 05:10:10
合計ジャッジ時間 5,702 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 51 ms
4,380 KB
testcase_07 AC 61 ms
4,380 KB
testcase_08 AC 73 ms
4,384 KB
testcase_09 AC 64 ms
4,380 KB
testcase_10 AC 56 ms
4,380 KB
testcase_11 AC 55 ms
4,384 KB
testcase_12 AC 51 ms
4,384 KB
testcase_13 AC 51 ms
4,380 KB
testcase_14 AC 38 ms
4,384 KB
testcase_15 AC 69 ms
4,384 KB
testcase_16 AC 59 ms
4,384 KB
testcase_17 AC 41 ms
4,380 KB
testcase_18 AC 73 ms
4,380 KB
testcase_19 AC 52 ms
4,380 KB
testcase_20 AC 67 ms
4,384 KB
testcase_21 AC 209 ms
22,592 KB
testcase_22 AC 192 ms
17,064 KB
testcase_23 AC 102 ms
4,380 KB
testcase_24 AC 102 ms
4,380 KB
testcase_25 AC 108 ms
4,380 KB
testcase_26 AC 106 ms
4,380 KB
testcase_27 AC 107 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

typedef long long ll;

int main() {
    int N;
    cin >> N;
    vector<int> a(N);
    rep(i, N) cin >> a[i];
    multiset<int> s1, s2;
    ll ans = 0;
    s1.insert(a[0]);
    s1.insert(a[1]);
    s2.insert(-a[0]);
    s2.insert(-a[1]);
    int l = 0;
    for (int i = 2; i < N; i++) {
        s1.insert(a[i]);
        s2.insert(-a[i]);
        int m1 = *s1.begin();
        s1.erase(s1.begin());
        int m2 = *s1.begin();
        int M = -*s2.begin();
        //cout << m1 << " " << m2 << " " << M << "\n";
        s1.insert(m1);
        if (M > m1 + m2) {
            ans += (s1.size() - 2) * (s1.size() - 1) / 2;
            while (M > m1 + m2) {
                s1.erase(s1.find(a[l]));
                s2.erase(s2.find((-a[l])));
                l++;
                m1 = *s1.begin();
                s1.erase(s1.begin());
                m2 = *s1.begin();
                M = -*s2.begin();
                s1.insert(m1);
            }
            ans -= (s1.size() - 2) * (s1.size() - 1) / 2;
        }
        //cout << "ans=" << ans << "\n";
    }
    ans += (s1.size() - 1) * s1.size() / 2;
    cout << ans << "\n";
}
0