結果

問題 No.837 Noelちゃんと星々2
コンテスト
ユーザー kimiyuki
提出日時 2019-06-14 21:43:43
言語 C++17(gcc12)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 850 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,712 ms
コンパイル使用メモリ 224,372 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-06-07 12:15:37
合計ジャッジ時間 16,918 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 2
other AC * 2 WA * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:28:17: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   28 |     int n; scanf("%d", &n);
      |            ~~~~~^~~~~~~~~~
main.cpp:31:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   31 |         scanf("%d", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~

ソースコード

diff #
raw source code

#pragma GCC optimize "O3"
#pragma GCC target "avx"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define ALL(x) begin(x), end(x)
using ll = long long;
using namespace std;
template <class T, class U> inline void chmin(T & a, U const & b) { a = min<T>(a, b); }

ll solve(int n, vector<int> & a) {
    sort(ALL(a));
    if (a.front() == a.back()) {
        return 1;
    }
    ll ans = LLONG_MAX;
    REP3 (m, 1, n) {
        ll sum = 0;
        REP (i, m) {
            sum += abs(a[i] - a[i < m ? m / 2 : m + (n - m) / 2]);
        }
        chmin(ans, sum);
    }
    return ans;
}

int main() {
    int n; scanf("%d", &n);
    vector<int> a(n);
    REP (i, n) {
        scanf("%d", &a[i]);
    }
    printf("%lld\n", solve(n, a));
    return 0;
}
0