結果

問題 No.2627 Unnatural Pitch
ユーザー zeta7532zeta7532
提出日時 2024-01-15 02:02:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 132 ms / 4,000 ms
コード長 1,552 bytes
コンパイル時間 2,428 ms
コンパイル使用メモリ 211,700 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-01-15 02:03:03
合計ジャッジ時間 6,527 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 4 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 124 ms
8,064 KB
testcase_05 AC 125 ms
8,064 KB
testcase_06 AC 97 ms
8,064 KB
testcase_07 AC 80 ms
6,676 KB
testcase_08 AC 89 ms
6,676 KB
testcase_09 AC 119 ms
8,064 KB
testcase_10 AC 120 ms
8,064 KB
testcase_11 AC 3 ms
6,676 KB
testcase_12 AC 3 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 3 ms
6,676 KB
testcase_15 AC 3 ms
6,676 KB
testcase_16 AC 127 ms
6,676 KB
testcase_17 AC 132 ms
7,552 KB
testcase_18 AC 128 ms
6,676 KB
testcase_19 AC 127 ms
6,676 KB
testcase_20 AC 45 ms
6,676 KB
testcase_21 AC 57 ms
6,676 KB
testcase_22 AC 97 ms
7,140 KB
testcase_23 AC 53 ms
6,676 KB
testcase_24 AC 74 ms
6,676 KB
testcase_25 AC 70 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
const ll mod = 998244353;
#define fi first
#define se second
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(x) x.begin(),x.end()
#define faster ios::sync_with_stdio(false);cin.tie(nullptr)

ll f(ll x,ll y){
    if(x<=0) return 0;
    return (x+y-1)/y;
}

ll g(ll x,ll y,ll z){
    ll ok=(z-x)/y-100,ng=(z-x)/y+100;
    while(ng-ok>1){
        ll mid=(ok+ng)/2;
        if(x+mid*y<z) ok=mid;
        else ng=mid;
    }
    return x+ng*y;
}

int main() {
    ll N,K,L,U;
    cin >> N >> K >> L >> U;
    vector<ll> A(N);
    rep(i,N) cin >> A[i];
    ll ok=-1,ng=1e12+500;
    while(ng-ok>1){
        ll x=(ok+ng)/2;
        ll a=0,b=0;
        rep(i,N){
            if(A[i]<x) a++;
            if(x+U-L<A[i]) b++;
        }
        if(a<=b) ok=x;
        else ng=x;
    }
    ll now=0;
    ll l=ok-K+1;
    ll u=l+U-L;
    rep(i,N){
        now+=max((ll)0,f(l-A[i],K));
        now+=max((ll)0,f(A[i]-u,K));
    }
    ll ans=now;
    vector<ll> cum(K*2-1,0);
    rep(i,N){
        ll xl=(A[i]+1)%K;
        ll minl=g(xl,K,max(A[i],ok-K+2));
        while(minl<=ok+K) cum[minl-(ok-K+2)]++,minl+=K;
        ll xu=(A[i]+L+K-U%K)%K;
        ll minu=g(xu,K,ok-K+2);
        while(minu<=ok+K){
            if(minu+U-L<=A[i]) cum[minu-(ok-K+2)]--;
            minu+=K;
        }    
    }
    rep(i,K*2-1){
        now+=cum[i];
        ans=min(ans,now);
    }
    cout << ans << endl;
    return 0;
}
0