結果

問題 No.2304 Distinct Elements
ユーザー milanis48663220milanis48663220
提出日時 2023-05-13 18:31:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 67 ms / 3,000 ms
コード長 3,054 bytes
コンパイル時間 1,185 ms
コンパイル使用メモリ 126,384 KB
実行使用メモリ 6,832 KB
最終ジャッジ日時 2023-08-19 16:22:30
合計ジャッジ時間 7,231 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 41 ms
6,696 KB
testcase_05 AC 42 ms
6,604 KB
testcase_06 AC 42 ms
6,788 KB
testcase_07 AC 42 ms
6,728 KB
testcase_08 AC 41 ms
6,604 KB
testcase_09 AC 67 ms
6,760 KB
testcase_10 AC 67 ms
6,792 KB
testcase_11 AC 66 ms
6,756 KB
testcase_12 AC 66 ms
6,620 KB
testcase_13 AC 67 ms
6,832 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 35 ms
6,364 KB
testcase_30 AC 4 ms
4,376 KB
testcase_31 AC 28 ms
5,016 KB
testcase_32 AC 52 ms
6,332 KB
testcase_33 AC 6 ms
4,380 KB
testcase_34 AC 54 ms
6,360 KB
testcase_35 AC 60 ms
6,608 KB
testcase_36 AC 5 ms
4,376 KB
testcase_37 AC 15 ms
4,380 KB
testcase_38 AC 46 ms
6,780 KB
testcase_39 AC 46 ms
6,732 KB
testcase_40 AC 26 ms
6,068 KB
testcase_41 AC 14 ms
4,808 KB
testcase_42 AC 35 ms
6,784 KB
testcase_43 AC 48 ms
6,700 KB
testcase_44 AC 48 ms
6,380 KB
testcase_45 AC 38 ms
5,096 KB
testcase_46 AC 28 ms
4,940 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 43 ms
6,804 KB
testcase_50 AC 46 ms
6,620 KB
testcase_51 AC 45 ms
6,756 KB
testcase_52 AC 40 ms
6,616 KB
testcase_53 AC 37 ms
6,804 KB
testcase_54 AC 37 ms
6,800 KB
testcase_55 AC 36 ms
6,664 KB
testcase_56 AC 35 ms
6,764 KB
testcase_57 AC 46 ms
6,596 KB
testcase_58 AC 44 ms
6,792 KB
testcase_59 AC 37 ms
6,604 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

template<typename T>
class SlopeTrick {
    public:
    SlopeTrick() {
        min_f = 0;
        add_l = 0;
        add_r = 0;
    }

    void add_pos(T a){ // add max(x-a, 0)
        l.push(a-add_l);
        T l0 = l.top()+add_l; l.pop();
        r.push(l0-add_r);
        min_f += max<T>(l0-a, 0);
    }

    void add_neg(T a){ // add max(a-x, 0)
        r.push(a-add_r);
        T r0 = r.top()+add_r; r.pop();
        l.push(r0-add_l);
        min_f += max<T>(a-r0, 0);
    }

    void slide_neg(T a){
        add_l += a;
    }

    void slide_pos(T a){
        add_r += a;
    }

    T get_min(){
        return min_f;
    }

    void clear_right(){
        while(!r.empty()) r.pop();
    }

    /**
     * be careful that this method takes O(NlonN) time (N := r.size()+l.size()).
     */ 
    T eval(T x) {
        T ans = min_f;
        vector<T> buf;
        while(!l.empty()){
            buf.push_back(l.top());
            l.pop();
        }
        for(T a: buf){
            l.push(a);
            ans += max<T>(a+add_l-x, 0);
        }
        buf.clear();
        while(!r.empty()){
            buf.push_back(r.top());
            r.pop();
        }
        for(T a: buf){
            r.push(a);
            ans += max<T>(x-a-add_r, 0);
        }
        return ans;
    }

    private:
    priority_queue<T> l;
    priority_queue<T, vector<T>, greater<T>> r;
    T min_f;
    T add_l;
    T add_r;
};


using P = pair<ll, ll>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a.begin(), a.end());
    for(int i = 0; i < n; i++) a[i] -= i;
    SlopeTrick<ll> st;
    for(int i = 0; i < n; i++){
        st.clear_right();
        st.add_pos(a[i]);
        st.add_neg(a[i]);
    }
    cout << st.get_min() << endl;
}
0