結果

問題 No.2304 Distinct Elements
ユーザー t98slider
提出日時 2023-05-13 23:34:02
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 69 ms / 3,000 ms
コード長 1,089 bytes
コンパイル時間 1,908 ms
コンパイル使用メモリ 178,440 KB
実行使用メモリ 6,316 KB
最終ジャッジ日時 2024-11-29 14:36:23
合計ジャッジ時間 5,518 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 58
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template<class T> struct slope_trick {
    T minv, offsetL, offsetR;
    std::priority_queue<T> pqL;
    std::priority_queue<T, std::vector<T>, std::greater<T>> pqR; 
    slope_trick() : minv(0), offsetL(0), offsetR(0) {}
    void add_relu(T x) {
        pqL.push(x - offsetL);
        minv += (pqL.top() + offsetL) - x;
        pqR.push(pqL.top() + offsetL - offsetR);
        pqL.pop();
    }
    void add_irelu(T x) {
        pqR.push(x - offsetR);
        minv += x - (pqR.top() + offsetR);
        pqL.push(pqR.top() + offsetR - offsetL);
        pqR.pop();
    }
    void L_shift(T x){ offsetL += x; }
    void R_shift(T x){ offsetR += x; }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    slope_trick<ll> st;
    vector<int> a(n);
    for(auto &&v : a) cin >> v;
    sort(a.begin(), a.end());
    for(int i = 0; i < n; i++){
        st.add_relu(a[i] - i);
        st.add_irelu(a[i] - i);
        while(!st.pqR.empty())st.pqR.pop();
    }
    cout << st.minv << '\n';
}
0