結果

問題 No.2627 Unnatural Pitch
ユーザー AngrySadEightAngrySadEight
提出日時 2024-02-04 12:18:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 547 ms / 4,000 ms
コード長 1,772 bytes
コンパイル時間 1,230 ms
コンパイル使用メモリ 87,268 KB
実行使用メモリ 70,528 KB
最終ジャッジ日時 2024-02-04 12:18:36
合計ジャッジ時間 9,416 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 402 ms
56,448 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 506 ms
70,528 KB
testcase_05 AC 533 ms
70,528 KB
testcase_06 AC 396 ms
45,568 KB
testcase_07 AC 82 ms
6,676 KB
testcase_08 AC 148 ms
17,408 KB
testcase_09 AC 501 ms
57,984 KB
testcase_10 AC 454 ms
57,984 KB
testcase_11 AC 3 ms
6,676 KB
testcase_12 AC 5 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 4 ms
6,676 KB
testcase_15 AC 3 ms
6,676 KB
testcase_16 AC 292 ms
28,544 KB
testcase_17 AC 547 ms
62,208 KB
testcase_18 AC 252 ms
21,504 KB
testcase_19 AC 369 ms
41,856 KB
testcase_20 AC 107 ms
14,464 KB
testcase_21 AC 323 ms
48,896 KB
testcase_22 AC 433 ms
59,648 KB
testcase_23 AC 292 ms
45,568 KB
testcase_24 AC 161 ms
19,328 KB
testcase_25 AC 317 ms
44,928 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;

    vector<ll> left_rem(K, 0);   // 左側の値の余り
    vector<ll> right_rem(K, 0);  // 右側の値の余り
    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