結果

問題 No.738 平らな農地
ユーザー rpy3cpp
提出日時 2019-05-16 00:17:26
言語 C++14
(gcc 8.2.0)
結果
AC  
実行時間 93 ms
コード長 2,648 Byte
コンパイル時間 1,593 ms
使用メモリ 8,152 KB
最終ジャッジ日時 2019-05-16 00:17:33

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
00_sample1.txt AC 3 ms
1,544 KB
00_sample2.txt AC 3 ms
1,544 KB
00_sample3.txt AC 3 ms
1,548 KB
00_sample4.txt AC 3 ms
1,544 KB
00_sample5.txt AC 2 ms
1,544 KB
10_random_small_00.txt AC 4 ms
1,584 KB
10_random_small_01.txt AC 5 ms
1,584 KB
10_random_small_02.txt AC 4 ms
1,668 KB
10_random_small_03.txt AC 3 ms
1,580 KB
10_random_small_04.txt AC 3 ms
1,568 KB
10_random_small_05.txt AC 2 ms
1,568 KB
10_random_small_06.txt AC 4 ms
1,596 KB
10_random_small_07.txt AC 4 ms
1,580 KB
10_random_small_08.txt AC 4 ms
1,580 KB
10_random_small_09.txt AC 2 ms
1,568 KB
11_random_large_00.txt AC 59 ms
4,192 KB
11_random_large_01.txt AC 59 ms
4,196 KB
11_random_large_02.txt AC 58 ms
3,136 KB
11_random_large_03.txt AC 58 ms
3,140 KB
11_random_large_04.txt AC 43 ms
2,080 KB
11_random_large_05.txt AC 66 ms
4,196 KB
11_random_large_06.txt AC 70 ms
2,872 KB
11_random_large_07.txt AC 66 ms
3,928 KB
11_random_large_08.txt AC 57 ms
2,612 KB
11_random_large_09.txt AC 50 ms
2,344 KB
20_anti_small_00.txt AC 3 ms
1,592 KB
20_anti_small_01.txt AC 4 ms
1,588 KB
20_anti_small_02.txt AC 4 ms
1,592 KB
20_anti_small_03.txt AC 4 ms
1,588 KB
20_anti_small_04.txt AC 4 ms
1,592 KB
20_anti_small_05.txt AC 4 ms
1,592 KB
20_anti_small_06.txt AC 4 ms
1,596 KB
20_anti_small_07.txt AC 3 ms
1,588 KB
20_anti_small_08.txt AC 3 ms
1,588 KB
20_anti_small_09.txt AC 4 ms
1,596 KB
20_anti_small_10.txt AC 3 ms
1,596 KB
20_anti_small_11.txt AC 4 ms
1,592 KB
20_anti_small_12.txt AC 3 ms
1,592 KB
20_anti_small_13.txt AC 3 ms
1,580 KB
20_anti_small_14.txt AC 2 ms
1,588 KB
20_anti_small_15.txt AC 3 ms
1,592 KB
20_anti_small_16.txt AC 4 ms
1,588 KB
20_anti_small_17.txt AC 3 ms
1,596 KB
20_anti_small_18.txt AC 3 ms
1,600 KB
20_anti_small_19.txt AC 3 ms
1,596 KB
21_anti_large_00.txt AC 63 ms
5,252 KB
21_anti_large_01.txt AC 59 ms
4,456 KB
21_anti_large_02.txt AC 59 ms
4,984 KB
21_anti_large_03.txt AC 55 ms
5,248 KB
21_anti_large_04.txt AC 54 ms
5,252 KB
21_anti_large_05.txt AC 55 ms
5,780 KB
21_anti_large_06.txt AC 64 ms
4,724 KB
21_anti_large_07.txt AC 59 ms
4,724 KB
21_anti_large_08.txt AC 62 ms
4,720 KB
21_anti_large_09.txt AC 65 ms
4,724 KB
21_anti_large_10.txt AC 72 ms
4,720 KB
21_anti_large_11.txt AC 73 ms
4,724 KB
21_anti_large_12.txt AC 74 ms
4,720 KB
21_anti_large_13.txt AC 61 ms
4,984 KB
21_anti_large_14.txt AC 64 ms
5,516 KB
21_anti_large_15.txt AC 62 ms
5,512 KB
21_anti_large_16.txt AC 58 ms
5,516 KB
21_anti_large_17.txt AC 81 ms
4,984 KB
21_anti_large_18.txt AC 93 ms
4,720 KB
21_anti_large_19.txt AC 62 ms
4,984 KB
31_corners_00.txt AC 25 ms
2,080 KB
31_corners_01.txt AC 25 ms
2,080 KB
32_corners_00.txt AC 40 ms
4,724 KB
32_corners_01.txt AC 41 ms
4,984 KB
33_corners_00.txt AC 35 ms
2,612 KB
33_corners_01.txt AC 45 ms
4,456 KB
34_corners_00.txt AC 35 ms
2,608 KB
34_corners_01.txt AC 29 ms
3,196 KB
34_corners_02.txt AC 31 ms
4,456 KB
34_corners_03.txt AC 28 ms
2,612 KB
35_corners_00.txt AC 35 ms
2,872 KB
35_corners_01.txt AC 38 ms
4,456 KB
36_corners_00.txt AC 31 ms
2,348 KB
36_corners_01.txt AC 43 ms
2,344 KB
37_corners_00.txt AC 42 ms
2,612 KB
38_corners_00.txt AC 46 ms
4,192 KB
39_corners_00.txt AC 40 ms
2,876 KB
40_corners_00.txt AC 29 ms
2,348 KB
41_corners_00.txt AC 44 ms
3,400 KB
42_corners_00.txt AC 49 ms
3,428 KB
43_corners_00.txt AC 27 ms
2,084 KB
44_corners_00.txt AC 27 ms
2,084 KB
45_corners_00.txt AC 33 ms
8,152 KB
45_corners_01.txt AC 32 ms
7,624 KB
90_hand_00.txt AC 3 ms
1,548 KB
90_hand_01.txt AC 3 ms
1,552 KB
90_hand_02.txt AC 3 ms
1,548 KB
テストケース一括ダウンロード

ソースコード

diff #
#include <bits/stdc++.h>
using namespace std;

template<typename T>
class MedianHeap{
private:
    T deviation;
    multiset<T> Ls;
    multiset<T> Rs;
    void adjust(){
        if (Ls.size() < Rs.size()){
            Ls.insert(*Rs.begin());
            Rs.erase(Rs.begin());
        }else if(Ls.size() > Rs.size() + 1){
            Rs.insert(*Ls.rbegin());
            Ls.erase(--Ls.end());
        }
    }
public:
    MedianHeap(const vector<T> & src): deviation(0){
        vector<T> tmp(src);
        sort(tmp.begin(), tmp.end());
        int n_L = src.size() - src.size() / 2;
        T med = *(tmp.begin() + n_L - 1);
        for (auto v : tmp) deviation += abs(v - med);
        Ls.insert(tmp.begin(), tmp.begin() + n_L);
        Rs.insert(tmp.begin() + n_L, tmp.end());
    }
    T median(){
        if (Ls.size() > Rs.size()){
            assert(Ls.size() == Rs.size() + 1);
            return *Ls.rbegin();
        }else{
            assert(Ls.size() == Rs.size());
            return (*Ls.rbegin() + *Rs.begin()) / 2LL;
        }
    }
    void insert(T v){
        T prev_med = this->median();
        if (v <= prev_med){
            Ls.insert(v);
        }else{
            Rs.insert(v);
        }
        this->adjust();
        if (Ls.size() > Rs.size()){
            deviation += abs(this->median() - v);
        }else{
            deviation += abs(prev_med - v);
        }
    }
    bool erase(T v){
        T prev_med = this->median();
        if (v > prev_med){
            auto it = Rs.find(v);
            if (it == Rs.end()) return false;
            Rs.erase(it);
        }else{
            auto it = Ls.find(v);
            if (it == Ls.end()) return false;
            Ls.erase(it);
        }
        this->adjust();
        if (Ls.size() > Rs.size()){
            deviation -= abs(this->median() - v);
        }else{
            deviation -= abs(prev_med - v);
        }
        return true;
    }
    T get_deviation(){
        return this->deviation;
    }
};


long long solve(int N, int K, const vector<long long> & As){
    vector<long long> src(As.begin(), As.begin() + K);
    MedianHeap<long long> mh(src);
    long long record = mh.get_deviation();
    for (int i = K; i < N; ++i){
        mh.insert(As[i]);
        if (not mh.erase(As[i - K])){
            cout << "Erase Failed! val = " << As[i - K] << endl;
        };
        record = min(record, mh.get_deviation());
    }
    return record;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, K;
    cin >> N >> K;
    vector<long long> As(N, 0);
    for (auto & a : As) cin >> a;
    cout << solve(N, K, As) << endl;
    return 0;
}
0