結果

問題 No.738 平らな農地
ユーザー rpy3cpp
提出日時 2019-05-15 19:20:15
言語 C++14
(gcc 8.3.0)
結果
RE   .
実行時間 -
コード長 3,631 Byte
コンパイル時間 1,660 ms
使用メモリ 8,916 KB
最終ジャッジ日時 2019-08-12 01:33:58

テストケース

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