結果

問題 No.609 Noelちゃんと星々
ユーザー dnishdnish
提出日時 2018-06-16 10:32:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 837 bytes
コンパイル時間 1,481 ms
コンパイル使用メモリ 165,436 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 05:59:48
合計ジャッジ時間 3,679 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
4,380 KB
testcase_01 AC 21 ms
4,380 KB
testcase_02 AC 15 ms
4,376 KB
testcase_03 AC 19 ms
4,376 KB
testcase_04 AC 10 ms
4,380 KB
testcase_05 AC 49 ms
4,380 KB
testcase_06 AC 42 ms
4,380 KB
testcase_07 AC 26 ms
4,380 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 17 ms
4,376 KB
testcase_10 AC 32 ms
4,376 KB
testcase_11 AC 29 ms
4,376 KB
testcase_12 AC 37 ms
4,376 KB
testcase_13 AC 8 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 44 ms
4,376 KB
testcase_16 AC 51 ms
4,376 KB
testcase_17 AC 40 ms
4,376 KB
testcase_18 AC 15 ms
4,376 KB
testcase_19 AC 43 ms
4,380 KB
testcase_20 AC 52 ms
4,380 KB
testcase_21 AC 53 ms
4,380 KB
testcase_22 AC 52 ms
4,376 KB
testcase_23 AC 53 ms
4,376 KB
testcase_24 AC 52 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define REP(i,n,N) for(ll i=(n); i<(N); i++)
#define RREP(i,n,N) for(int i=(N-1); i>=n; i--)
#define CK(n,a,b) (a)<=(n)&&(n)<(b)
#define ALL(v) (v).begin(),(v).end()
#define p(s) cout<<(s)<<endl
typedef long long ll;
using namespace std;
const ll inf=1e18;

ll N;
int a[100010];
ll search(ll mid){
    ll ret=0;
    REP(i,0,N){
        ret += abs(a[i]-mid);
    }
    return ret;
}
int main(){

    while(cin>>N){

        REP(i,0,N) cin>>a[i];
        ll lb=-1e9-1, ub=1e9;
        while(ub -lb > 3){
            ll mid1=(lb * 2 + ub) / 3; 
            ll mid2=(lb + ub * 2) / 3; 
            if(search(mid1) > search(mid2)) lb=mid1; 
            else ub = mid2;
        }
        ll ans = inf;
        for(ll i=lb; i<ub; i++){
            ans=min(ans,search(i));
        }
        p(ans);
    }
    return 0;
}
0