結果

問題 No.837 Noelちゃんと星々2
ユーザー Enjapma_kyoproEnjapma_kyopro
提出日時 2019-06-14 21:43:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 3,294 bytes
コンパイル時間 772 ms
コンパイル使用メモリ 95,736 KB
実行使用メモリ 21,372 KB
最終ジャッジ日時 2023-09-02 07:19:27
合計ジャッジ時間 2,813 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
21,120 KB
testcase_01 AC 55 ms
21,276 KB
testcase_02 AC 54 ms
21,372 KB
testcase_03 AC 53 ms
21,152 KB
testcase_04 AC 54 ms
21,124 KB
testcase_05 AC 54 ms
21,112 KB
testcase_06 AC 54 ms
21,196 KB
testcase_07 AC 53 ms
21,128 KB
testcase_08 AC 54 ms
21,144 KB
testcase_09 AC 7 ms
21,152 KB
testcase_10 AC 6 ms
21,236 KB
testcase_11 AC 6 ms
21,148 KB
testcase_12 AC 6 ms
21,160 KB
testcase_13 AC 6 ms
21,144 KB
testcase_14 AC 5 ms
21,148 KB
testcase_15 AC 5 ms
21,236 KB
testcase_16 AC 6 ms
21,196 KB
testcase_17 AC 5 ms
21,340 KB
testcase_18 AC 6 ms
21,208 KB
testcase_19 AC 5 ms
21,208 KB
testcase_20 AC 6 ms
21,364 KB
testcase_21 AC 5 ms
21,200 KB
testcase_22 AC 6 ms
21,164 KB
testcase_23 AC 7 ms
21,148 KB
testcase_24 AC 6 ms
21,112 KB
testcase_25 AC 5 ms
21,156 KB
testcase_26 AC 5 ms
19,104 KB
testcase_27 AC 5 ms
21,156 KB
testcase_28 AC 5 ms
21,240 KB
testcase_29 AC 6 ms
21,212 KB
testcase_30 AC 6 ms
21,156 KB
testcase_31 AC 6 ms
19,160 KB
testcase_32 AC 8 ms
21,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <climits>
#include <bitset>
#include <stack>
#include <queue>
#include <iomanip>
#include <limits>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string.h>
#include <tuple>
#include <time.h>
#include <random>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> P;

long long int INF = 3e18;
double Pi = 3.1415926535897932384626;

vector<ll> G[500005];
//vector<P> tree[500010];
priority_queue <ll> pql;
priority_queue <P> pqp;
//big priority queue
priority_queue <ll,vector<ll>,greater<ll> > pqls;
priority_queue <P,vector<P>,greater<P> > pqps;
//small priority queue
//top pop

int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,-1,1};
char dir[] = "DRUL";
//ll bit[500005];
//↓,→,↑,←


#define p(x) cout<<x<<endl;
#define el cout<<endl;
#define pe(x) cout<<(x)<<" ";
#define ps(x) cout<<fixed<<setprecision(25)<<x<<endl;
#define pu(x) cout<<(x);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout<< #x << " = " << x <<endl


ll mod = 1000000007;
ll rui(ll number1,ll number2){

    if(number2 == 0){
        return 1;
    }else{
        ll number3 = rui(number1,number2 / 2);
        number3 *= number3;
        number3 %= mod;
        if(number2%2==1){
            number3 *= number1;
            number3 %= mod;
        }
        return number3;
    }
}
ll gcd(ll number1,ll number2){

    if(number1 > number2){
        swap(number1,number2);
    }
    if(number1 == 0 || number1 == number2){
        return number2;
    }else{
        return gcd(number2 % number1,number1);
    }
}
void YES(bool condition){

    if(condition){
        p("YES");
    }else{
        p("NO");
    }
    return;
}
void Yes(bool condition){

    if(condition){
        p("Yes");
    }else{
        p("No");
    }
    return;
}

//ll fact[800005],rfact[800005];
/*
void c3_init(){
    fact[0] = rfact[0] = 1;
    for(ll i=1; i<=800000; i++){
        fact[i] = (fact[i-1]*i) % mod;
    }
	rfact[800000] = rui(fact[800000],mod - 2);
	for(ll i=800000; i>=1; i--){
	   rfact[i-1] = rfact[i] * i;
	   rfact[i-1] %= mod;
	}
	return;}
ll c3(ll n,ll r){
    return (((fact[n] * rfact[r]) % mod) * rfact[n-r]) % mod;}
*/

ll n,m,num,ans,a,b,c,d,e,g,h,w,i,j,k,q,l,r,idx;
ll x[500005],y[500005],z[500005];
char s[500005];

bool flag,dame;

ll midnum(ll left,ll right){
	return (left+right)/2;
}

ll sum(ll left,ll right){
	if(left == 0){
		return y[right];
	}else{
		return y[right] - y[left-1];
	}
}


ll midsum(ll left,ll right,ll mid){
	//[left,right]の各xについてabs(x-mid)の合計
	ll res1 = sum(mid,right) - x[mid] * (right-mid+1);
	ll res2 = x[mid] * (mid-left+1) - sum(left,mid);
	return res1 + res2;
}




int main(){
	cin >> n;
	for(i=0;i<n;i++){
		cin >> x[i];
	}
	sort(x,x+n);
	if(x[0] == x[n-1]){
		p(1);
		return 0;
	}
	for(i=0;i<n;i++){
		if(i != 0)y[i] = y[i-1] + x[i];
		else y[i] = x[i];
	}
	ans = INF;
	for(i=0;i<n-1;i++){
		ll left1,right1,left2,right2;
		left1 = 0;
		right1 = i;
		left2 = i+1;
		right2 = n-1;
		ll mid1,mid2;
		mid1 = midnum(left1,right1);
		mid2 = midnum(left2,right2);
		ans = min(ans,midsum(left1,right1,mid1)+midsum(left2,right2,mid2));
		//p(ans);
	}
	p(ans);

	return 0;
}
0