結果

問題 No.2627 Unnatural Pitch
ユーザー ぷらぷら
提出日時 2024-02-09 23:10:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,012 bytes
コンパイル時間 1,644 ms
コンパイル使用メモリ 150,836 KB
実行使用メモリ 33,080 KB
最終ジャッジ日時 2024-02-09 23:10:12
合計ジャッジ時間 4,933 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
6,676 KB
testcase_04 AC 106 ms
22,096 KB
testcase_05 AC 108 ms
22,096 KB
testcase_06 AC 104 ms
23,080 KB
testcase_07 AC 77 ms
20,416 KB
testcase_08 AC 125 ms
29,944 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 4 ms
6,676 KB
testcase_12 AC 4 ms
6,676 KB
testcase_13 WA -
testcase_14 AC 3 ms
6,676 KB
testcase_15 AC 3 ms
6,676 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

template <typename T>
struct BinaryIndexedTree {
    int N;
    vector<T>bit;
    BinaryIndexedTree(){}
    BinaryIndexedTree(int x) {
        N = x;
        bit.resize(x+1);
    }
    void add(int x,T y) {
        x++;
        while(x <= N) {
            bit[x] += y;
            x += x&-x;
        }
    }
    T sum(int x) {
        x++;
        T res = 0;
        while(x) {
            res += bit[x];
            x -= x&-x;
        }
        return res;
    }
    inline T sum(int l, int r) {//[l,r)
        return sum(r-1)-sum(l-1);
    }
    inline T operator[](int k) {
        return sum(k)-sum(k-1);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,K;
    long long L,U;
    cin >> N >> K >> L >> U;
    vector<long long>A(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }
    BinaryIndexedTree<int>bit(K);
    sort(A.begin(),A.end());
    vector<long long>sum(N);
    vector<vector<array<int,2>>>query(N+1);
    long long res = 0;
    for(int i = 0; i < N; i++) {
        sum[i] += (A[i]/K)*i;
        sum[i] -= res;
        sum[i] += bit.sum(0,A[i]%K);
        bit.add(A[i]%K,1);
        res += A[i]/K;
        int it = upper_bound(A.begin(),A.end(),A[i]+(U-L))-A.begin();
        sum[i] -= (A[i]+U-L)/K*(N-it);
        query[it].push_back({(int)((A[i]+U-L)%K),i});
    }
    BinaryIndexedTree<int>bit2(K);
    res = 0;
    for(int i = N-1; i >= 0; i--) {
        res += A[i]/K;
        bit2.add(A[i]%K,1);
        for(auto j:query[i]) {
            sum[j[1]] += res;
            sum[j[1]] += bit2.sum(j[0]+1,K);
        }
    }
    vector<long long>sum2(N);
    vector<vector<array<int,2>>>query2(N);
    BinaryIndexedTree<int>bit3(K);
    res = 0;
    for(int i = N-1; i >= 0; i--) {
        sum2[i] -= (A[i]/K)*(N-1-i);
        sum2[i] += res;
        sum2[i] += bit3.sum(A[i]%K+1,K);
        bit3.add(A[i]%K,1);
        res += A[i]/K;
        int it = lower_bound(A.begin(),A.end(),A[i]-(U-L))-A.begin()-1;
        if(it == -1) continue;
        sum2[i] += (A[i]-(U-L))/K*(it+1);
        query2[it].push_back({(int)((A[i]-(U-L))%K),i});
    }
    BinaryIndexedTree<int>bit4(K);
    res = 0;
    for(int i = 0; i < N; i++) {
        res += A[i]/K;
        bit4.add(A[i]%K,1);
        for(auto j:query2[i]) {
            sum2[j[1]] -= res;
            sum2[j[1]] += bit4.sum(0,j[0]);
        }
    }
    cout << min(*min_element(sum.begin(),sum.end()),*min_element(sum2.begin(),sum2.end())) << "\n";
}
0