結果

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

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
00_sample1.txt AC 4 ms
3,436 KB
00_sample2.txt AC 0 ms
3,320 KB
00_sample3.txt AC 4 ms
3,368 KB
00_sample4.txt AC 0 ms
3,288 KB
00_sample5.txt AC 0 ms
3,368 KB
10_random_small_00.txt AC 4 ms
3,400 KB
10_random_small_01.txt AC 4 ms
3,396 KB
10_random_small_02.txt AC 4 ms
3,484 KB
10_random_small_03.txt RE -
10_random_small_04.txt AC 4 ms
3,300 KB
10_random_small_05.txt AC 0 ms
3,392 KB
10_random_small_06.txt AC 4 ms
3,416 KB
10_random_small_07.txt RE -
10_random_small_08.txt AC 4 ms
3,488 KB
10_random_small_09.txt AC 0 ms
3,452 KB
11_random_large_00.txt RE -
11_random_large_01.txt AC 60 ms
5,416 KB
11_random_large_02.txt AC 52 ms
4,472 KB
11_random_large_03.txt RE -
11_random_large_04.txt AC 40 ms
3,632 KB
11_random_large_05.txt RE -
11_random_large_06.txt RE -
11_random_large_07.txt AC 60 ms
5,264 KB
11_random_large_08.txt AC 56 ms
4,096 KB
11_random_large_09.txt RE -
20_anti_small_00.txt AC 4 ms
3,344 KB
20_anti_small_01.txt RE -
20_anti_small_02.txt AC 0 ms
3,364 KB
20_anti_small_03.txt AC 4 ms
3,476 KB
20_anti_small_04.txt AC 4 ms
3,492 KB
20_anti_small_05.txt AC 0 ms
3,368 KB
20_anti_small_06.txt RE -
20_anti_small_07.txt AC 4 ms
3,392 KB
20_anti_small_08.txt AC 0 ms
3,392 KB
20_anti_small_09.txt AC 4 ms
3,420 KB
20_anti_small_10.txt RE -
20_anti_small_11.txt AC 4 ms
3,368 KB
20_anti_small_12.txt AC 4 ms
3,420 KB
20_anti_small_13.txt RE -
20_anti_small_14.txt AC 4 ms
3,368 KB
20_anti_small_15.txt AC 0 ms
3,368 KB
20_anti_small_16.txt AC 4 ms
3,484 KB
20_anti_small_17.txt RE -
20_anti_small_18.txt AC 4 ms
3,420 KB
20_anti_small_19.txt AC 0 ms
3,400 KB
21_anti_large_00.txt AC 60 ms
6,276 KB
21_anti_large_01.txt AC 60 ms
5,412 KB
21_anti_large_02.txt AC 56 ms
6,324 KB
21_anti_large_03.txt AC 52 ms
6,544 KB
21_anti_large_04.txt AC 56 ms
6,528 KB
21_anti_large_05.txt AC 56 ms
6,840 KB
21_anti_large_06.txt AC 64 ms
6,048 KB
21_anti_large_07.txt AC 56 ms
6,008 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 60 ms
6,008 KB
21_anti_large_12.txt AC 56 ms
5,752 KB
21_anti_large_13.txt RE -
21_anti_large_14.txt AC 60 ms
6,468 KB
21_anti_large_15.txt AC 72 ms
6,920 KB
21_anti_large_16.txt AC 68 ms
6,584 KB
21_anti_large_17.txt AC 64 ms
5,940 KB
21_anti_large_18.txt RE -
21_anti_large_19.txt AC 60 ms
6,392 KB
31_corners_00.txt RE -
31_corners_01.txt RE -
32_corners_00.txt AC 40 ms
5,848 KB
32_corners_01.txt AC 40 ms
6,372 KB
33_corners_00.txt AC 32 ms
4,268 KB
33_corners_01.txt AC 44 ms
5,724 KB
34_corners_00.txt AC 20 ms
3,904 KB
34_corners_01.txt AC 24 ms
4,696 KB
34_corners_02.txt AC 28 ms
5,468 KB
34_corners_03.txt AC 24 ms
4,168 KB
35_corners_00.txt AC 36 ms
4,208 KB
35_corners_01.txt AC 36 ms
5,584 KB
36_corners_00.txt AC 28 ms
3,556 KB
36_corners_01.txt AC 32 ms
3,572 KB
37_corners_00.txt WA -
38_corners_00.txt WA -
39_corners_00.txt RE -
40_corners_00.txt AC 24 ms
3,556 KB
41_corners_00.txt AC 28 ms
4,696 KB
42_corners_00.txt AC 24 ms
4,752 KB
43_corners_00.txt AC 24 ms
3,620 KB
44_corners_00.txt RE -
45_corners_00.txt AC 32 ms
9,016 KB
45_corners_01.txt AC 32 ms
8,360 KB
90_hand_00.txt AC 4 ms
3,352 KB
90_hand_01.txt AC 0 ms
3,392 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