結果

問題 No.738 平らな農地
ユーザー face4face4
提出日時 2020-01-30 14:37:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 5,994 bytes
コンパイル時間 1,024 ms
コンパイル使用メモリ 85,784 KB
実行使用メモリ 8,460 KB
最終ジャッジ日時 2023-10-14 08:27:05
合計ジャッジ時間 9,377 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,356 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 84 ms
5,532 KB
testcase_16 AC 89 ms
5,448 KB
testcase_17 AC 89 ms
4,476 KB
testcase_18 AC 87 ms
4,768 KB
testcase_19 AC 66 ms
4,348 KB
testcase_20 AC 92 ms
5,172 KB
testcase_21 AC 92 ms
4,424 KB
testcase_22 AC 93 ms
5,100 KB
testcase_23 AC 88 ms
4,348 KB
testcase_24 AC 75 ms
4,352 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,356 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 2 ms
4,352 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,352 KB
testcase_43 AC 2 ms
4,356 KB
testcase_44 AC 2 ms
4,352 KB
testcase_45 AC 97 ms
6,336 KB
testcase_46 AC 91 ms
5,428 KB
testcase_47 AC 91 ms
5,960 KB
testcase_48 AC 85 ms
6,228 KB
testcase_49 AC 80 ms
6,480 KB
testcase_50 AC 88 ms
6,492 KB
testcase_51 AC 99 ms
5,760 KB
testcase_52 AC 91 ms
5,756 KB
testcase_53 AC 87 ms
6,000 KB
testcase_54 AC 97 ms
6,080 KB
testcase_55 AC 98 ms
5,764 KB
testcase_56 AC 87 ms
5,752 KB
testcase_57 AC 85 ms
5,740 KB
testcase_58 AC 88 ms
6,080 KB
testcase_59 AC 92 ms
6,544 KB
testcase_60 AC 93 ms
6,420 KB
testcase_61 AC 88 ms
6,284 KB
testcase_62 AC 86 ms
6,084 KB
testcase_63 AC 94 ms
5,708 KB
testcase_64 AC 90 ms
6,076 KB
testcase_65 AC 14 ms
4,348 KB
testcase_66 AC 14 ms
4,348 KB
testcase_67 AC 47 ms
5,744 KB
testcase_68 AC 48 ms
6,028 KB
testcase_69 AC 58 ms
4,352 KB
testcase_70 AC 57 ms
5,496 KB
testcase_71 AC 60 ms
4,352 KB
testcase_72 AC 69 ms
4,644 KB
testcase_73 AC 77 ms
5,484 KB
testcase_74 AC 72 ms
4,424 KB
testcase_75 AC 55 ms
4,620 KB
testcase_76 AC 56 ms
5,364 KB
testcase_77 AC 52 ms
4,348 KB
testcase_78 AC 59 ms
4,352 KB
testcase_79 AC 70 ms
4,360 KB
testcase_80 AC 62 ms
5,432 KB
testcase_81 AC 63 ms
4,368 KB
testcase_82 AC 66 ms
4,348 KB
testcase_83 AC 94 ms
4,756 KB
testcase_84 AC 88 ms
5,156 KB
testcase_85 AC 19 ms
4,348 KB
testcase_86 AC 20 ms
4,348 KB
testcase_87 AC 59 ms
8,460 KB
testcase_88 AC 51 ms
8,008 KB
testcase_89 AC 1 ms
4,348 KB
testcase_90 AC 1 ms
4,348 KB
testcase_91 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<random>
#include<vector>
#include<climits>
using namespace std;

// randをやめた方が良いかもしれない
// 0-indexed
template<class T, class F>
struct Treap{
    struct Node{
        T val, sum;
        int pri;
        int cnt;
        Node* ch[2];
        Node(T v, int p) : val(v), sum(v), pri(p){
            cnt = 1;
            ch[0] = ch[1] = NULL;
        }
    };

    Node* root;
    vector<T> v;
    F f;    
    T def;  
    Treap(T def, F f) : f(f), def(def) {
        root = NULL;
    }

    int count(Node* t)  {return t==NULL ? 0 : t->cnt;}
    T sum(Node* t)    {return t==NULL ? def : t->sum;}

    Node* update(Node* t){
        t->cnt = count(t->ch[0]) + count(t->ch[1]) + 1;
        t->sum = f(sum(t->ch[0]), f(t->val, sum(t->ch[1])));
        return t;
    }

    Node* rotate(Node* t, int b){
        Node* s = t->ch[b];
        t->ch[b] = s->ch[1-b];
        s->ch[1-b] = t;
        update(t);
        update(s);
        return s;
    }

    Node* insert(Node* t, int k, T val, int pri){
        if(t == NULL)   return new Node(val, pri);
        int leftCnt = count(t->ch[0]);
        int b = k > leftCnt;
        t->ch[b] = insert(t->ch[b], k-(b ? leftCnt+1 : 0), val, pri);
        update(t);
        if(t->ch[b]->pri > t->pri)  t = rotate(t, b);
        return t;
    }
    Node* insert_value(Node* t, T val, int pri){
        if(t == NULL)   return new Node(val, pri);
        int b = val > t->val;
        t->ch[b] = insert_value(t->ch[b], val, pri);
        update(t);
        if(t->ch[b]->pri > t->pri)  t = rotate(t, b);
        return t;
    }
    Node* erase(Node* t, int k, bool me=false){
        if(!me && count(t) <= k)   return t;    // out-of-range
        me |= count(t->ch[0])==k;
        if(me){
            if(t->ch[0]==NULL && t->ch[1]==NULL){
                delete t;
                return NULL;
            }
            int b = (t->ch[0]==NULL ? -1 : t->ch[0]->pri) < (t->ch[1]==NULL ? -1 : t->ch[1]->pri);
            t = rotate(t, b);
            t->ch[1-b] = erase(t->ch[1-b], k, me);
        }else{
            if(count(t->ch[0]) > k){
                t->ch[0] = erase(t->ch[0], k, me);
            }else{  // definitely count(t->ch[0]) < k
                t->ch[1] = erase(t->ch[1], k-count(t->ch[0])-1, me);
            }
        }
        return update(t);
    }
    Node* erase_value(Node* t, T val, bool me=false){
        if(t == NULL)   return NULL;    // there's no node whose value is val
        me |= t->val==val;
        if(me){
            if(t->ch[0]==NULL && t->ch[1]==NULL){
                delete t;
                return NULL;
            }
            int b = (t->ch[0]==NULL ? -1 : t->ch[0]->pri) < (t->ch[1]==NULL ? -1 : t->ch[1]->pri);
            t = rotate(t, b);
            t->ch[1-b] = erase_value(t->ch[1-b], val, me);
        }else{
            if(val < t->val){
                t->ch[0] = erase_value(t->ch[0], val, me);
            }else{  // definitely count(t->ch[0]) < k
                t->ch[1] = erase_value(t->ch[1], val, me);
            }
        }
        return update(t);
    }

    Node* merge(Node* l, Node* r){
        Node* tmp = new Node(def, INT_MAX);    // rand()は環境依存
        tmp->ch[0] = l;
        tmp->ch[1] = r;
        update(tmp);
        bool me = true;
        return root = erase(tmp, -1, me);
    }

    // [0, k), [k, n)
    pair<Node*, Node*> split(int k){
        root = insert(root, k, def, INT_MAX);
        return make_pair(root->ch[0], root->ch[1]);
    }

    T get(Node* t, int k){
        int leftCnt = count(t->ch[0]);
        if(leftCnt > k)     return get(t->ch[0], k);
        if(leftCnt == k)    return t->val;
        return get(t->ch[1], k-1-leftCnt);
    }

    void set(Node* t, int k, T newVal){
        if(k < count(t->ch[0])){
            set(t->ch[0], k, newVal);
        }else if(k == count(t->ch[0])){
            t->val = newVal; // or f(t->val, newVal) //
        }else if(k > count(t->ch[0])){
            set(t->ch[1], k-count(t->ch[0])-1, newVal);
        }
        update(t);
    }

    // a, bはt-oriented
    T query(Node* t, int a, int b){
        if(t == NULL)   return def;
        if(a == 0 && count(t) <= b+1)   return t->sum;
        T ret = def;
        int leftCnt = count(t->ch[0]);
        if(a < leftCnt) ret = f(ret, query(t->ch[0], a, b));
        if(a <= leftCnt && leftCnt <= b)    ret = f(ret, t->val);
        if(b > leftCnt) ret = f(ret, query(t->ch[1], max(0, a-leftCnt-1), b-leftCnt-1));
        return ret;
    }

    Node* insert(int k, T val){
        return root = insert(root, k, val, rand());
    }
    Node* erase(int k){
        return root = erase(root, k);
    }
    T get(int k){
        return get(root, k);
    }
    void set(int k, T val){
        set(root, k, val);
    }
    T query(int a, int b){
        return query(root, a, b);
    }
    Node* insert_value(T val){
        return root = insert_value(root, val, rand());
    }
    Node* erase_value(T val){
        return root = erase_value(root, val);
    }
    void flatten(Node* t){
        if(t==NULL) return;
        if(t->ch[0] != NULL)    flatten(t->ch[0]);
        v.push_back(t->val);
        if(t->ch[1] != NULL)    flatten(t->ch[1]);
    }

    vector<T> flatten(){
        v.clear();
        flatten(root);
        return v;
    }
};

typedef long long ll;

int main(){
    auto f = [](ll x, ll y)->ll{
        return x+y;
    };
    Treap<ll, decltype(f)> t(0, f);
    int n, k;
    scanf("%d %d", &n, &k);
    ll ans = 1ll<<60;
    vector<ll> v(n);
    for(int i = 0; i < n; i++)  scanf("%lld", v.begin()+i);
    for(int i = 0; i < n; i++){
        if(i >= k)  t.erase_value(v[i-k]);
        t.insert_value(v[i]);
        if(i < k-1) continue;
        ll med = t.get(k/2);
        ll tmp = 0;
        if(k/2 > 0) tmp += k/2*med-t.query(0,k/2-1);
        if(k/2+1 <= k-1)    tmp += t.query(k/2+1,k-1)-(k-1-k/2)*med;
        ans = min(ans, tmp);
    }
    printf("%lld\n", ans);
    return 0;
}
0