結果

問題 No.738 平らな農地
ユーザー ミドリムシミドリムシ
提出日時 2018-09-28 23:04:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 4,337 bytes
コンパイル時間 1,089 ms
コンパイル使用メモリ 86,036 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2024-04-20 11:56:49
合計ジャッジ時間 7,999 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 85 ms
5,632 KB
testcase_16 AC 93 ms
5,760 KB
testcase_17 AC 91 ms
5,376 KB
testcase_18 AC 90 ms
5,376 KB
testcase_19 AC 84 ms
5,376 KB
testcase_20 AC 89 ms
5,504 KB
testcase_21 AC 97 ms
5,376 KB
testcase_22 AC 89 ms
5,376 KB
testcase_23 AC 94 ms
5,376 KB
testcase_24 AC 88 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 3 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 95 ms
6,528 KB
testcase_46 AC 93 ms
5,888 KB
testcase_47 AC 96 ms
6,400 KB
testcase_48 AC 87 ms
6,400 KB
testcase_49 AC 86 ms
6,528 KB
testcase_50 AC 88 ms
6,784 KB
testcase_51 AC 98 ms
6,144 KB
testcase_52 AC 95 ms
6,144 KB
testcase_53 AC 95 ms
6,272 KB
testcase_54 AC 99 ms
6,272 KB
testcase_55 AC 99 ms
6,016 KB
testcase_56 AC 96 ms
6,144 KB
testcase_57 AC 88 ms
6,144 KB
testcase_58 AC 93 ms
6,400 KB
testcase_59 AC 94 ms
6,656 KB
testcase_60 AC 91 ms
6,784 KB
testcase_61 AC 91 ms
6,656 KB
testcase_62 AC 89 ms
6,272 KB
testcase_63 AC 106 ms
6,144 KB
testcase_64 AC 99 ms
6,272 KB
testcase_65 AC 40 ms
5,376 KB
testcase_66 AC 43 ms
5,376 KB
testcase_67 AC 104 ms
6,784 KB
testcase_68 AC 107 ms
7,168 KB
testcase_69 AC 99 ms
5,376 KB
testcase_70 AC 115 ms
6,528 KB
testcase_71 AC 20 ms
5,376 KB
testcase_72 AC 40 ms
5,376 KB
testcase_73 AC 37 ms
5,376 KB
testcase_74 AC 40 ms
5,376 KB
testcase_75 AC 98 ms
5,376 KB
testcase_76 AC 109 ms
6,528 KB
testcase_77 AC 73 ms
5,376 KB
testcase_78 AC 82 ms
5,376 KB
testcase_79 AC 118 ms
5,376 KB
testcase_80 AC 130 ms
6,272 KB
testcase_81 AC 83 ms
5,376 KB
testcase_82 AC 75 ms
5,376 KB
testcase_83 AC 33 ms
5,376 KB
testcase_84 AC 34 ms
5,376 KB
testcase_85 AC 52 ms
5,376 KB
testcase_86 AC 50 ms
5,376 KB
testcase_87 AC 77 ms
9,984 KB
testcase_88 AC 78 ms
9,728 KB
testcase_89 AC 1 ms
5,376 KB
testcase_90 AC 1 ms
5,376 KB
testcase_91 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void POSS(T condition){ if(condition) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; }
template<class T> inline void Poss(T condition){ if(condition) cout << "Possible" << endl; else cout << "Impossible" << endl; }
template<class T> inline void First(T condition){ if(condition) cout << "First" << endl; else cout << "Second" << endl; }
template<class T = string, class U = char>int character_count(T text, U character){ int ans = 0; for(U i: text){ ans += (i == character); } return ans; }
long power(long base, long exponent, long module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ long root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int y, x; }; position move_pattern[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); }
template<class T, class U> string to_string(pair<T, U> x){ return to_string(x.first) + "," + to_string(x.second); }
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++){ ans += to_string(*i) + " "; } ans.pop_back(); cout << ans << endl; }
template<class itr> void cins(itr start, itr goal){ for(auto i = start; i != goal; i++){ cin >> (*i); } }
template<class T> T gcd(T a, T b){ if(a && b){ return gcd(min(a, b), max(a, b) % min(a, b)); }else{ return a; }} template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
#define mod long(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl(long(n))
#define fcout cout << fixed << setprecision(10)
#define highest(x) (63 - __builtin_clzl(x))

template<class T>
struct sum_set{
    size_t size = 0;
    long sum = 0;
    map<long, size_t, T> array;
    
    void hidden_push(long x){
        array[x]++;
    }
    
    void push(long x){
        size++;
        sum += x;
        array[x]++;
    }
    
    void pop(){
        size--;
        sum -= top();
        array[top()]--;
        if(!array[top()]){
            array.erase(top());
        }
    }
    
    void pop(long x){
        size--;
        sum -= x;
        array[x]--;
        if(!array[x]){
            array.erase(x);
        }
    }

    long top(){
        return (*array.begin()).first;
    }
};

int main(){
    int N, K;
    cin >> N >> K;
    sum_set<greater<long>> less_middle; // first (K + 1) / 2
    sum_set<less<long>> greater_middle; // last K / 2
    less_middle.hidden_push(-1e18);
    greater_middle.hidden_push(1e18);
    long A[N];
    for(int i = 0; i < K; i++){
        cin >> A[i];
        less_middle.push(A[i]);
    }
    for(int i = 0; i < K / 2; i++){
        greater_middle.push(less_middle.top());
        less_middle.pop();
    }
    long ans = (less_middle.top() * less_middle.size - less_middle.sum) + (greater_middle.sum - less_middle.top() * greater_middle.size);
    for(int i = K; i < N; i++){
        cin >> A[i];
        if(A[i - K] <= less_middle.top()){
            less_middle.pop(A[i - K]);
            long tmp[3] = {less_middle.top(), A[i], greater_middle.top()};
            less_middle.pop();
            greater_middle.pop();
            sort(tmp, tmp + 3);
            less_middle.push(tmp[0]);
            less_middle.push(tmp[1]);
            greater_middle.push(tmp[2]);
        }else{
            greater_middle.pop(A[i - K]);
            long tmp[3] = {less_middle.top(), A[i], greater_middle.top()};
            less_middle.pop();
            greater_middle.pop();
            sort(tmp, tmp + 3);
            less_middle.push(tmp[0]);
            greater_middle.push(tmp[1]);
            greater_middle.push(tmp[2]);
        }
        ans = min(ans, long(less_middle.top() * less_middle.size - less_middle.sum) + long(greater_middle.sum - less_middle.top() * greater_middle.size));
    }
    cout << ans << endl;
}
0