結果

問題 No.609 Noelちゃんと星々
ユーザー kichirb3kichirb3
提出日時 2018-03-24 10:27:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 1,316 bytes
コンパイル時間 661 ms
コンパイル使用メモリ 76,112 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-07 08:22:50
合計ジャッジ時間 2,952 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
4,380 KB
testcase_01 AC 14 ms
4,376 KB
testcase_02 AC 10 ms
4,380 KB
testcase_03 AC 13 ms
4,380 KB
testcase_04 AC 7 ms
4,380 KB
testcase_05 AC 31 ms
4,380 KB
testcase_06 AC 27 ms
4,380 KB
testcase_07 AC 17 ms
4,384 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 11 ms
4,380 KB
testcase_10 AC 20 ms
4,376 KB
testcase_11 AC 19 ms
4,380 KB
testcase_12 AC 23 ms
4,380 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 28 ms
4,380 KB
testcase_16 AC 33 ms
4,380 KB
testcase_17 AC 25 ms
4,380 KB
testcase_18 AC 10 ms
4,380 KB
testcase_19 AC 28 ms
4,380 KB
testcase_20 AC 33 ms
4,380 KB
testcase_21 AC 33 ms
4,376 KB
testcase_22 AC 33 ms
4,376 KB
testcase_23 AC 33 ms
4,380 KB
testcase_24 AC 34 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// No.609 Noelちゃんと星々
// https://yukicoder.me/problems/no/609
//

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

long long solve(vector<int> &stars);
long long calc_score(int n, vector<int>& stars);


int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    unsigned int N;
    cin >> N;
    vector<int> stars(N);
    for (auto i = 0; i < N; ++i)
        cin >> stars[i];
    long long ans = solve(stars);
    cout << ans << endl;
}

long long calc_score(int n, vector<int>& stars) {
    long long ans = 0;
    for (auto s: stars) {
        ans += abs(s - n);
    }
    return ans;
}


long long solve(vector<int> &stars) {
    int ub = *max_element(stars.begin(), stars.end());
    int lb = *min_element(stars.begin(), stars.end());

    for (auto i = 0; i < 100; ++i) {
        int mid1 = (lb * 2 + ub) / 3;
        long long mid1_score = calc_score(mid1, stars);
        int mid2 = (lb + ub * 2) / 3;
        long long mid2_score = calc_score(mid2, stars);

        if (mid2_score > mid1_score)
            ub = mid2;
        else
            lb = mid1;
    }

    long long ans = pow(2, 60);
    for (int i = lb; i <= ub; ++i) {
        long long t = calc_score(i, stars);
        ans = min(ans, t);
    }
    return ans;
}
0