結果

問題 No.2627 Unnatural Pitch
ユーザー AngrySadEightAngrySadEight
提出日時 2024-01-02 01:33:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 999 ms / 4,000 ms
コード長 1,761 bytes
コンパイル時間 1,108 ms
コンパイル使用メモリ 89,748 KB
実行使用メモリ 92,416 KB
最終ジャッジ日時 2024-01-02 01:35:47
合計ジャッジ時間 13,262 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 828 ms
78,464 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 987 ms
92,416 KB
testcase_05 AC 999 ms
92,416 KB
testcase_06 AC 838 ms
67,456 KB
testcase_07 AC 82 ms
6,676 KB
testcase_08 AC 149 ms
17,408 KB
testcase_09 AC 816 ms
79,872 KB
testcase_10 AC 839 ms
79,872 KB
testcase_11 AC 4 ms
6,676 KB
testcase_12 AC 5 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 5 ms
6,676 KB
testcase_15 AC 4 ms
6,676 KB
testcase_16 AC 360 ms
33,024 KB
testcase_17 AC 786 ms
80,768 KB
testcase_18 AC 313 ms
23,168 KB
testcase_19 AC 543 ms
51,840 KB
testcase_20 AC 142 ms
17,152 KB
testcase_21 AC 541 ms
65,280 KB
testcase_22 AC 773 ms
78,464 KB
testcase_23 AC 489 ms
60,800 KB
testcase_24 AC 215 ms
22,528 KB
testcase_25 AC 499 ms
59,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <vector>
using namespace std;
using ll = long long;

int main(){
    ll N, K, L, U;
    cin >> N >> K >> L >> U;
    vector<ll> A(N);
    for (ll i = 0; i < N; i++) {
        cin >> A[i];
    }
    ll diffs = U - L;
    // 「L より小さいものの個数」 <= 「U より大きいものの個数」
    ll ok = 0;
    ll ng = 1000000000002;
    while (ng - ok > 1) {
        ll mid = (ok + ng) / 2;
        ll lower = 0;
        ll upper = 0;
        for (ll i = 0; i < N; i++) {
            if (A[i] < mid) {
                lower++;
            }
            if (A[i] > mid + diffs) {
                upper++;
            }
        }
        if (lower <= upper) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    map<ll, ll> mp;
    for (ll i = 0; i < N; i++) {
        mp[A[i]]++;
    }
    ll ans = 1000000000000000002;

    ll now_score = 0;
    ll now_L = ok - K;
    ll now_U = now_L + diffs;

    map<ll, ll> left_rem;   // 左側の値の余り
    map<ll, ll> right_rem;  // 右側の値の余り
    for (ll i = 0; i < N; i++) {
        if (A[i] < now_L) {
            now_score += ((now_L - A[i] + K - 1) / K);
            left_rem[A[i] % K]++;
        }
        if (A[i] > now_U) {
            now_score += ((A[i] - now_U + K - 1) / K);
            right_rem[A[i] % K]++;
        }
    }
    ans = min(ans, now_score);
    for (ll i = 0; i < 2 * K; i++) {
        left_rem[(now_L % K + K) % K] += mp[now_L];
        now_score += left_rem[(now_L % K + K) % K];
        now_L++;
        now_U++;
        now_score -= right_rem[(now_U % K + K) % K];
        right_rem[(now_U % K + K) % K] -= mp[now_U];
        ans = min(ans, now_score);
    }
    cout << ans << endl;
}
0