結果

問題 No.1435 Mmm......
ユーザー ktr216ktr216
提出日時 2021-03-19 23:43:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 231 ms / 2,000 ms
コード長 1,231 bytes
コンパイル時間 2,343 ms
コンパイル使用メモリ 172,228 KB
実行使用メモリ 22,656 KB
最終ジャッジ日時 2024-04-29 19:41:34
合計ジャッジ時間 5,273 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 59 ms
5,376 KB
testcase_07 AC 71 ms
5,376 KB
testcase_08 AC 82 ms
5,376 KB
testcase_09 AC 75 ms
5,376 KB
testcase_10 AC 66 ms
5,376 KB
testcase_11 AC 65 ms
5,376 KB
testcase_12 AC 60 ms
5,376 KB
testcase_13 AC 59 ms
5,376 KB
testcase_14 AC 45 ms
5,376 KB
testcase_15 AC 83 ms
5,376 KB
testcase_16 AC 71 ms
5,376 KB
testcase_17 AC 51 ms
5,376 KB
testcase_18 AC 85 ms
5,376 KB
testcase_19 AC 64 ms
5,376 KB
testcase_20 AC 78 ms
5,376 KB
testcase_21 AC 231 ms
22,656 KB
testcase_22 AC 211 ms
17,152 KB
testcase_23 AC 123 ms
5,376 KB
testcase_24 AC 124 ms
5,376 KB
testcase_25 AC 124 ms
5,376 KB
testcase_26 AC 123 ms
5,376 KB
testcase_27 AC 124 ms
5,376 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