結果

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

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
00_sample1.txt AC 2 ms
6,872 KB
00_sample2.txt AC 3 ms
6,868 KB
00_sample3.txt AC 2 ms
8,912 KB
00_sample4.txt AC 2 ms
6,872 KB
00_sample5.txt AC 2 ms
6,868 KB
10_random_small_00.txt AC 5 ms
6,868 KB
10_random_small_01.txt AC 3 ms
6,872 KB
10_random_small_02.txt AC 4 ms
6,872 KB
10_random_small_03.txt AC 3 ms
6,868 KB
10_random_small_04.txt AC 3 ms
6,872 KB
10_random_small_05.txt AC 2 ms
6,872 KB
10_random_small_06.txt AC 3 ms
6,872 KB
10_random_small_07.txt AC 3 ms
6,868 KB
10_random_small_08.txt AC 4 ms
6,868 KB
10_random_small_09.txt AC 4 ms
6,872 KB
11_random_large_00.txt AC 59 ms
6,868 KB
11_random_large_01.txt AC 59 ms
6,872 KB
11_random_large_02.txt AC 57 ms
8,912 KB
11_random_large_03.txt AC 57 ms
6,868 KB
11_random_large_04.txt AC 43 ms
6,868 KB
11_random_large_05.txt AC 60 ms
6,872 KB
11_random_large_06.txt AC 60 ms
6,872 KB
11_random_large_07.txt AC 60 ms
6,872 KB
11_random_large_08.txt AC 56 ms
8,920 KB
11_random_large_09.txt AC 49 ms
6,868 KB
20_anti_small_00.txt AC 2 ms
6,872 KB
20_anti_small_01.txt AC 3 ms
6,872 KB
20_anti_small_02.txt AC 3 ms
8,912 KB
20_anti_small_03.txt AC 4 ms
6,872 KB
20_anti_small_04.txt AC 3 ms
6,872 KB
20_anti_small_05.txt AC 2 ms
6,872 KB
20_anti_small_06.txt AC 3 ms
6,868 KB
20_anti_small_07.txt AC 2 ms
6,872 KB
20_anti_small_08.txt AC 3 ms
6,868 KB
20_anti_small_09.txt AC 3 ms
6,868 KB
20_anti_small_10.txt AC 2 ms
6,872 KB
20_anti_small_11.txt AC 3 ms
6,872 KB
20_anti_small_12.txt AC 4 ms
6,872 KB
20_anti_small_13.txt AC 3 ms
6,868 KB
20_anti_small_14.txt AC 3 ms
6,872 KB
20_anti_small_15.txt AC 3 ms
6,872 KB
20_anti_small_16.txt AC 3 ms
6,868 KB
20_anti_small_17.txt AC 3 ms
6,872 KB
20_anti_small_18.txt AC 4 ms
6,872 KB
20_anti_small_19.txt AC 3 ms
6,872 KB
21_anti_large_00.txt AC 63 ms
6,868 KB
21_anti_large_01.txt AC 58 ms
6,868 KB
21_anti_large_02.txt AC 61 ms
6,868 KB
21_anti_large_03.txt AC 55 ms
8,920 KB
21_anti_large_04.txt AC 56 ms
6,868 KB
21_anti_large_05.txt AC 57 ms
6,868 KB
21_anti_large_06.txt AC 64 ms
6,872 KB
21_anti_large_07.txt AC 59 ms
6,872 KB
21_anti_large_08.txt AC 63 ms
6,868 KB
21_anti_large_09.txt AC 66 ms
8,920 KB
21_anti_large_10.txt AC 66 ms
6,872 KB
21_anti_large_11.txt AC 62 ms
6,868 KB
21_anti_large_12.txt AC 60 ms
6,872 KB
21_anti_large_13.txt AC 62 ms
6,872 KB
21_anti_large_14.txt AC 60 ms
6,868 KB
21_anti_large_15.txt AC 60 ms
6,868 KB
21_anti_large_16.txt AC 59 ms
8,912 KB
21_anti_large_17.txt AC 57 ms
6,872 KB
21_anti_large_18.txt AC 66 ms
6,872 KB
21_anti_large_19.txt AC 64 ms
6,872 KB
31_corners_00.txt AC 23 ms
6,868 KB
31_corners_01.txt AC 24 ms
6,868 KB
32_corners_00.txt AC 41 ms
6,872 KB
32_corners_01.txt AC 41 ms
6,872 KB
33_corners_00.txt AC 34 ms
6,868 KB
33_corners_01.txt AC 45 ms
6,868 KB
34_corners_00.txt AC 36 ms
6,872 KB
34_corners_01.txt AC 29 ms
6,868 KB
34_corners_02.txt AC 31 ms
6,872 KB
34_corners_03.txt AC 27 ms
6,868 KB
35_corners_00.txt AC 35 ms
6,868 KB
35_corners_01.txt AC 38 ms
8,916 KB
36_corners_00.txt AC 31 ms
6,872 KB
36_corners_01.txt AC 34 ms
6,868 KB
37_corners_00.txt AC 35 ms
6,872 KB
38_corners_00.txt AC 40 ms
6,872 KB
39_corners_00.txt AC 35 ms
6,868 KB
40_corners_00.txt AC 29 ms
6,872 KB
41_corners_00.txt AC 46 ms
6,868 KB
42_corners_00.txt AC 49 ms
6,872 KB
43_corners_00.txt AC 27 ms
6,872 KB
44_corners_00.txt AC 28 ms
6,872 KB
45_corners_00.txt AC 33 ms
8,156 KB
45_corners_01.txt AC 33 ms
7,624 KB
90_hand_00.txt AC 2 ms
6,872 KB
90_hand_01.txt AC 3 ms
6,872 KB
90_hand_02.txt AC 2 ms
6,868 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