結果

問題 No.738 平らな農地
ユーザー rpy3cpprpy3cpp
提出日時 2019-05-15 19:20:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,631 bytes
コンパイル時間 1,866 ms
コンパイル使用メモリ 180,500 KB
実行使用メモリ 8,800 KB
最終ジャッジ日時 2023-10-12 09:05:40
合計ジャッジ時間 11,779 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 RE -
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 RE -
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 RE -
testcase_16 AC 53 ms
5,624 KB
testcase_17 AC 49 ms
4,580 KB
testcase_18 RE -
testcase_19 AC 36 ms
4,348 KB
testcase_20 RE -
testcase_21 RE -
testcase_22 AC 55 ms
5,496 KB
testcase_23 AC 49 ms
4,368 KB
testcase_24 RE -
testcase_25 AC 2 ms
4,356 KB
testcase_26 RE -
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,348 KB
testcase_31 RE -
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 RE -
testcase_36 AC 2 ms
4,356 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 RE -
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 RE -
testcase_43 AC 2 ms
4,352 KB
testcase_44 AC 2 ms
4,352 KB
testcase_45 AC 57 ms
6,540 KB
testcase_46 AC 53 ms
5,636 KB
testcase_47 AC 57 ms
6,420 KB
testcase_48 AC 50 ms
6,728 KB
testcase_49 AC 50 ms
6,988 KB
testcase_50 AC 52 ms
6,992 KB
testcase_51 AC 61 ms
6,200 KB
testcase_52 AC 51 ms
6,260 KB
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 AC 56 ms
5,892 KB
testcase_57 AC 55 ms
5,900 KB
testcase_58 RE -
testcase_59 AC 56 ms
6,904 KB
testcase_60 AC 57 ms
7,064 KB
testcase_61 AC 55 ms
6,684 KB
testcase_62 AC 53 ms
6,372 KB
testcase_63 RE -
testcase_64 AC 56 ms
6,536 KB
testcase_65 RE -
testcase_66 RE -
testcase_67 AC 42 ms
5,996 KB
testcase_68 AC 42 ms
6,520 KB
testcase_69 AC 41 ms
4,468 KB
testcase_70 AC 47 ms
5,904 KB
testcase_71 AC 20 ms
4,352 KB
testcase_72 AC 28 ms
4,684 KB
testcase_73 AC 29 ms
5,756 KB
testcase_74 AC 27 ms
4,348 KB
testcase_75 AC 38 ms
4,472 KB
testcase_76 AC 38 ms
5,740 KB
testcase_77 AC 31 ms
4,348 KB
testcase_78 AC 33 ms
4,352 KB
testcase_79 WA -
testcase_80 WA -
testcase_81 RE -
testcase_82 AC 25 ms
4,352 KB
testcase_83 AC 30 ms
4,828 KB
testcase_84 AC 29 ms
4,888 KB
testcase_85 AC 19 ms
4,348 KB
testcase_86 RE -
testcase_87 AC 31 ms
8,800 KB
testcase_88 AC 30 ms
8,576 KB
testcase_89 AC 2 ms
4,348 KB
testcase_90 AC 2 ms
4,348 KB
testcase_91 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

long long solve_odd(int N, int K, const vector<long long> & As){
    int w = K / 2;
    vector<long long> Bs(As.begin(), As.begin() + K);
    sort(Bs.begin(), Bs.end());
    long long med = Bs[w];
    multiset<long long> Ls(Bs.begin(), Bs.begin() + w);
    multiset<long long> Rs(Bs.begin() + w + 1, Bs.end());
    long long cost = 0;
    for (auto b : Bs) cost += abs(b - med);
    long long best = cost;
    for (int i = K; i < N; ++i){
        long long vin = As[i];
        long long vout = As[i - K];
        if (med < vin and med < vout){
            Rs.insert(vin);
            auto it = Rs.find(vout);
            Rs.erase(it);
            cost += vin - vout;
        }else if (vin <= med and vout <= med){
            Ls.insert(vin);
            auto it = Ls.find(vout);
            Ls.erase(it);
            cost += vout - vin;
        }else if (vin <= med and med < vout){
            Rs.insert(med);
            auto it = Rs.find(vout);
            Rs.erase(it);
            cost += med - vout;
            med = *Ls.rbegin();
            Ls.insert(vin);
            it = Ls.find(med);
            Ls.erase(it);
            cost += med - vin;
        }else if (med < vin and vout <= med){
            Ls.insert(med);
            auto it = Ls.find(vout);
            Ls.erase(it);
            cost += vout - med;
            med = *Rs.begin();
            Rs.insert(vin);
            it = Rs.find(med);
            Rs.erase(it);
            cost += vin - med;
        }
        best = min(best, cost);
    }
    return best;
}

long long solve_even(int N, int K, const vector<long long> & As){
    int w = K / 2;
    vector<long long> Bs(As.begin(), As.begin() + K);
    sort(Bs.begin(), Bs.end());
    long long medL = Bs[w - 1];
    long long medR = Bs[w];
    long long med = (medL + medR) / 2LL;
    multiset<long long> Ls(Bs.begin(), Bs.begin() + w);
    multiset<long long> Rs(Bs.begin() + w, Bs.end());
    long long cost = 0;
    for (auto b : Bs) cost += abs(b - med);
    long long best = cost;
    for (int i = K; i < N; ++i){
        long long vin = As[i];
        long long vout = As[i - K];
        if (medL < vin and medR <= vout){
            Rs.insert(vin);
            auto it = Rs.find(vout);
            Rs.erase(it);
            cost += vin - vout;
        }else if (vin <= medR and vout <= medL){
            Ls.insert(vin);
            auto it = Ls.find(vout);
            assert(it != Ls.end());
            Ls.erase(it);
            cost += vout - vin;
        }else if (vin <= medR and medR <= vout){
            Rs.insert(medL);
            auto it = Rs.find(vout);
            Rs.erase(it);
            cost += medL - vout;
            Ls.insert(vin);
            it = Ls.find(medL);
            Ls.erase(it);
            cost += medL - vin;
        }else if (medL < vin and vout <= medL){
            Ls.insert(medR);
            auto it = Ls.find(vout);
            Ls.erase(it);
            cost += vout - medR;
            Rs.insert(vin);
            it = Rs.find(medR);
            Rs.erase(it);
            cost += vin - medR;
        }
        best = min(best, cost);
        medL = *Ls.rbegin();
        medR = *Rs.begin();
    }
    return best;
}

long long solve(int N, int K, const vector<long long> & As){
    if (K % 2 == 1){
        return solve_odd(N, K, As);
    }else{
        return solve_even(N, K, As);
    }
}

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