結果

問題 No.738 平らな農地
ユーザー raven7959raven7959
提出日時 2022-03-01 15:01:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 376 ms / 2,000 ms
コード長 6,246 bytes
コンパイル時間 2,515 ms
コンパイル使用メモリ 221,672 KB
実行使用メモリ 20,420 KB
最終ジャッジ日時 2023-09-22 04:23:05
合計ジャッジ時間 21,728 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 6 ms
4,380 KB
testcase_06 AC 7 ms
4,376 KB
testcase_07 AC 7 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 294 ms
12,228 KB
testcase_16 AC 297 ms
12,352 KB
testcase_17 AC 289 ms
11,564 KB
testcase_18 AC 291 ms
11,560 KB
testcase_19 AC 277 ms
11,556 KB
testcase_20 AC 303 ms
12,224 KB
testcase_21 AC 319 ms
12,000 KB
testcase_22 AC 300 ms
12,348 KB
testcase_23 AC 308 ms
11,704 KB
testcase_24 AC 286 ms
11,288 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 4 ms
4,376 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 3 ms
4,384 KB
testcase_30 AC 3 ms
4,376 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 3 ms
4,380 KB
testcase_33 AC 4 ms
4,380 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 3 ms
4,376 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 3 ms
4,380 KB
testcase_38 AC 3 ms
4,376 KB
testcase_39 AC 3 ms
4,376 KB
testcase_40 AC 3 ms
4,380 KB
testcase_41 AC 3 ms
4,376 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 4 ms
4,376 KB
testcase_44 AC 3 ms
4,380 KB
testcase_45 AC 338 ms
14,604 KB
testcase_46 AC 321 ms
13,408 KB
testcase_47 AC 323 ms
14,324 KB
testcase_48 AC 290 ms
14,200 KB
testcase_49 AC 309 ms
13,808 KB
testcase_50 AC 298 ms
14,596 KB
testcase_51 AC 329 ms
14,196 KB
testcase_52 AC 305 ms
13,544 KB
testcase_53 AC 316 ms
13,812 KB
testcase_54 AC 328 ms
14,212 KB
testcase_55 AC 331 ms
14,024 KB
testcase_56 AC 329 ms
13,868 KB
testcase_57 AC 306 ms
13,276 KB
testcase_58 AC 324 ms
14,076 KB
testcase_59 AC 322 ms
14,596 KB
testcase_60 AC 309 ms
14,656 KB
testcase_61 AC 314 ms
14,716 KB
testcase_62 AC 300 ms
13,544 KB
testcase_63 AC 339 ms
14,464 KB
testcase_64 AC 323 ms
14,420 KB
testcase_65 AC 228 ms
11,012 KB
testcase_66 AC 244 ms
11,208 KB
testcase_67 AC 227 ms
17,292 KB
testcase_68 AC 226 ms
18,036 KB
testcase_69 AC 309 ms
16,316 KB
testcase_70 AC 265 ms
18,348 KB
testcase_71 AC 17 ms
4,376 KB
testcase_72 AC 21 ms
4,380 KB
testcase_73 AC 20 ms
4,376 KB
testcase_74 AC 22 ms
4,376 KB
testcase_75 AC 265 ms
15,660 KB
testcase_76 AC 217 ms
17,544 KB
testcase_77 AC 277 ms
15,136 KB
testcase_78 AC 306 ms
16,312 KB
testcase_79 AC 376 ms
16,752 KB
testcase_80 AC 288 ms
17,792 KB
testcase_81 AC 264 ms
16,140 KB
testcase_82 AC 266 ms
15,920 KB
testcase_83 AC 22 ms
4,380 KB
testcase_84 AC 25 ms
4,380 KB
testcase_85 AC 326 ms
16,044 KB
testcase_86 AC 302 ms
15,000 KB
testcase_87 AC 298 ms
20,420 KB
testcase_88 AC 273 ms
19,744 KB
testcase_89 AC 2 ms
4,376 KB
testcase_90 AC 2 ms
4,380 KB
testcase_91 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define yn(joken) cout<<((joken) ? "Yes" : "No")<<endl
#define YN(joken) cout<<((joken) ? "YES" : "NO")<<endl
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<double>>;
const int INF = 1e9;
const ll LINF = 1e18;
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        os << v[i];
        if (i < int(v.size()) - 1) os << ' ';
    }
    return os;
}

// すでに追加したものを削除できるslope trick
// Query query(): Query{lx,rx,min_f}を返す,[lx,rx]でmin_fを取る
// add_all(T a): f(x)にaを加算する
// add_a_minus_x(T a): f(x)にmax(a-x,0)を加算する, \_
// add_x_minus_a(T a): f(x)にmax(x-a,0)を加算する, _/
// add_abs(T a): f(x)に|x-a|を加算する, \/
// erase_a_minus_x(T a): f(x)からmax(a-x,0)を削除する, \_
// erase_x_minus_a(T a): f(x)からmax(x-a,0)を削除する, _/
// erase_abs(T a): f(x)から|x-a|を削除する, \/
// clear_right(): f(x)<-min(f(t)),t<=x , \_/ -> \_
// clear_left(): f(x)<-min(f(t)),x<=t , \_/ -> _/
// shift(T a,T b): f(x)<-min(f(t)),x-b<=t<=x-a
// shift(T a): f(x)<-f(x-a),shift(a,a)してるだけ
// get(T x): f(x)を返す, fを破壊しO(sz(L)+sz(R))
// merge(SlopeTrick x): f(x)にg(x)を加算する, gを破壊する

template <typename T>
struct ErasableSlopeTrick{
    const T INF = numeric_limits<T>::max() / 3;

    set<T> S_l,S_r;
    map<T,T> mp_l,mp_r;
    T min_f, offset_l, offset_r;

private:
    void insert_R(const T &a){
        S_r.insert(a - offset_r);
        mp_r[a - offset_r]++;
    }

    T top_R() const{
        if (S_r.empty()) return INF;
        else return *S_r.begin() + offset_r;
    }

    T pop_R(){
        T val = top_R();
        if (!S_r.empty()){
            mp_r[val]--;
            if(mp_r[val]==0) S_r.erase(val);
        }
        return val;
    }

    void insert_L(const T &a){
        S_l.insert(a - offset_l);
        mp_l[a - offset_l]++;
    }

    T top_L() const{
        if (S_l.empty()) return -INF;
        else return *--S_l.end() + offset_l;
    }

    T pop_L(){
        T val = top_L();
        if (!S_l.empty()){
            mp_l[val]--;
            if(mp_l[val]==0) S_l.erase(val);
        }
        return val;
    }

    size_t size(){
        return S_l.size() + S_r.size();
    }

public:
    ErasableSlopeTrick() : min_f(0), offset_l(0), offset_r(0) {}

    struct Query{
        T lx, rx, min_f;
    };

    Query query() const{
        return (Query){top_L(), top_R(), min_f};
    }

    void add_all(const T &a){
        min_f += a;
    }

    void add_a_minus_x(const T &a){
        insert_R(a);
        insert_L(pop_R());
        min_f += max(T(0), a - top_L());
    }

    void add_x_minus_a(const T &a){
        insert_L(a);
        insert_R(pop_L());
        min_f += max(T(0), top_R() - a);
    }

    void add_abs(const T &a){
        add_a_minus_x(a);
        add_x_minus_a(a);
    }

    void erase_a_minus_x(const T &a){
        if(S_l.find(a - offset_l)==S_l.end()){
            mp_r[a - offset_r]--;
            if(mp_r[a - offset_r]==0) S_r.erase(a - offset_r);
            insert_R(pop_L());
        }
        else{
            mp_l[a - offset_l]--;
            if(mp_l[a - offset_l]==0) S_l.erase(a - offset_l);
        }
        min_f -= max(T(0), a - top_R());
    }

    void erase_x_minus_a(const T &a){
        if(S_l.find(a - offset_l)==S_l.end()){
            mp_r[a - offset_r]--;
            if(mp_r[a - offset_r]==0) S_r.erase(a - offset_r);
        }
        else{
            mp_l[a - offset_l]--;
            if(mp_l[a- offset_l]==0) S_l.erase(a - offset_l);
            insert_L(pop_R());
        }
        min_f -= max(T(0), top_L() - a);
    }

    void erase_abs(const T &a){
        erase_a_minus_x(a);
        erase_x_minus_a(a);
    }

    void clear_right(){
        if(!S_r.empty()) S_r.clear();
    }

    void clear_left(){
        if(!S_l.empty()) S_l.clear();
    }

    void shift(const T &a, const T &b){
        assert(a <= b);
        offset_l += a;
        offset_r += b;
    }

    void shift(const T &a){
        shift(a, a);
    }

    T get(const T &x){
        T ret = min_f;
        while (!S_l.empty()) ret += max(T(0), pop_L() - x);
        while (!S_r.empty()) ret += max(T(0), x - pop_R());
        return ret;
    }

    void merge(ErasableSlopeTrick &st){
        if (st.size() > size()){
            swap(st.S_l, S_l);
            swap(st.S_r, S_r);
            swap(st.min_f, min_f);
            swap(st.offset_l, offset_l);
            swap(st.offset_r, offset_r);
        }
        while (!st.S_r.empty()) add_x_minus_a(st.pop_R());
        while (!st.S_l.empty()) add_a_minus_x(st.pop_L());
        min_f += st.min_f;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N,K;
    cin>>N>>K;
    vl A(N);
    cin>>A;
    queue<ll> Q;
    ErasableSlopeTrick<ll> EST;
    ll ans=LINF;
    rep(i,N){
        EST.add_abs(A[i]);
        Q.push(A[i]);
        while(sz(Q)>K){
            auto a=Q.front();
            EST.erase_abs(a);
            Q.pop();
        }
        if(sz(Q)==K){
            auto ret=EST.query();
            chmin(ans,ret.min_f);
        }
    }
    cout<<ans<<endl;
}
0