結果

問題 No.609 Noelちゃんと星々
ユーザー Naoyk1212Naoyk1212
提出日時 2017-12-12 20:09:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 903 bytes
コンパイル時間 644 ms
コンパイル使用メモリ 62,380 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 23:28:42
合計ジャッジ時間 2,613 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
4,376 KB
testcase_01 AC 24 ms
4,376 KB
testcase_02 AC 16 ms
4,376 KB
testcase_03 AC 22 ms
4,376 KB
testcase_04 AC 11 ms
4,380 KB
testcase_05 AC 55 ms
4,376 KB
testcase_06 AC 47 ms
4,376 KB
testcase_07 AC 29 ms
4,376 KB
testcase_08 AC 8 ms
4,380 KB
testcase_09 AC 19 ms
4,376 KB
testcase_10 AC 35 ms
4,380 KB
testcase_11 AC 33 ms
4,376 KB
testcase_12 AC 41 ms
4,376 KB
testcase_13 AC 10 ms
4,380 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 48 ms
4,376 KB
testcase_16 AC 56 ms
4,376 KB
testcase_17 AC 45 ms
4,384 KB
testcase_18 AC 16 ms
4,380 KB
testcase_19 AC 48 ms
4,376 KB
testcase_20 AC 58 ms
4,376 KB
testcase_21 AC 57 ms
4,380 KB
testcase_22 AC 58 ms
4,380 KB
testcase_23 AC 58 ms
4,380 KB
testcase_24 AC 58 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>

int N;
std::vector<int> v(1e5);

long long solve(long long mid){
    long long cnt = 0;
    for(int i = 0; i < N; i++){
        cnt += std::abs(v[i] - mid);
    }
    return cnt;
}

long long solve2(int low, int high){
    long long min = 1e18;
    for(int i = low; i <= high; i++){
        long long ans = 0;
        for(int j = 0; j < N; j++){
            ans += std::abs(v[j] - i);
        }
        min = std::min(min, ans);
    }
    return min;
}

int main(){
    std::cin >> N;
    for(int i = 0; i < N; i++){
        std::cin >> v[i];
    }

    long long high = 1e9;
    long long low = -1e9;
    for(int i = 0; i < 100; i++){
        long long midu = (2 * high + low) / 3;
		long long midl = (high + 2 * low) / 3;

		if(solve(midl) < solve(midu))high = midu;
		else low = midl;
    }

    std::cout << solve2(low, high) << std::endl;
}
0