結果

問題 No.609 Noelちゃんと星々
コンテスト
ユーザー kichirb3
提出日時 2018-03-24 10:56:55
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 969 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 654 ms
コンパイル使用メモリ 100,112 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-03-11 07:16:09
合計ジャッジ時間 1,745 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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

long long int solve(vector<int> &stars, int N);
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 (int i = 0; i < N; ++i)
        cin >> stars[i];

    long long ans = solve(stars, N);
    cout << ans << endl;
}

long long int solve(vector<int> &stars, int N) {
    sort(stars.begin(), stars.end());

    int median;
    if (N % 2 == 1)
        median = stars[N/2];
    else
        median = (stars[(N-1)/2] + stars[N/2]) / 2;

    long long ans = calc_score(median, stars);
    return ans;
}


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