結果

問題 No.837 Noelちゃんと星々2
ユーザー kenta255kenta255
提出日時 2019-06-15 18:03:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 2,077 bytes
コンパイル時間 3,064 ms
コンパイル使用メモリ 206,672 KB
実行使用メモリ 6,892 KB
最終ジャッジ日時 2023-09-02 07:25:52
合計ジャッジ時間 4,907 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
6,852 KB
testcase_01 AC 53 ms
6,856 KB
testcase_02 AC 53 ms
6,784 KB
testcase_03 AC 54 ms
6,832 KB
testcase_04 AC 53 ms
6,788 KB
testcase_05 AC 53 ms
6,892 KB
testcase_06 AC 53 ms
6,796 KB
testcase_07 AC 53 ms
6,788 KB
testcase_08 AC 53 ms
6,856 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(a%b==0)return b;return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}//saisyo kobaisu
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=1e18+7;
const ll MOD=1e9+7;
#define out(a) cout<<fixed<<setprecision((a))

vector<ll> minc(ll n,vector<ll> y){
	vector<ll> sum(n,y[0]),minc(n,0);
	for(int i=1;i<n;i++)sum[i] = sum[i-1] + y[i];
	int i = 0;
	for(int j=1;j<n;j++){
		ll ci = (i+1)*y[i]-sum[i] + ((sum[j]-sum[i])-y[i]*(j-i));
		i++;
		ll cip = (i+1)*y[i]-sum[i] + ((sum[j]-sum[i])-y[i]*(j-i));
		i--;
		while((i<j)&&(ci>=cip)){
			i++;
			ci = cip;
			if(i==j)break;
			i++;
			cip = (i+1)*y[i]-sum[i] + ((sum[j]-sum[i])-y[i]*(j-i));
			i--;
		}
		minc[j] = ci;
	}
	return minc;
}
vector<ll> mincr(ll n,vector<ll> y){
	vector<ll> sum(n,y[n-1]),minc(n,0);
	for(int i=n-2;i>=0;i--)sum[i] = sum[i+1] + y[i];
	int i = n-1;
	for(int j=n-2;j>=0;j--){
		ll ci = sum[i]-(n-i)*y[i] + (i-j)*y[i]-(sum[j]-sum[i]);
		i--;
		ll cip = sum[i]-(n-i)*y[i] + (i-j)*y[i]-(sum[j]-sum[i]);
		i++;
		while((i>j)&&(ci>=cip)){
			i--;
			ci = cip;
			if(i==j)break;
			i--;
			cip = sum[i]-(n-i)*y[i] + (i-j)*y[i]-(sum[j]-sum[i]);
			i++;
		}
		minc[j] = ci;
	}
	return minc;
}


int main(){
	ll N;
	cin >> N;
	vector<ll> Y(N),minf,minr;
	FOR(i,0,N)cin>>Y[i];
	SORT(Y);
	if(Y[0]==Y[N-1]){
		cout << 1 << endl;
		return 0;
	}
	minf = minc(N,Y);
	minr = mincr(N,Y);
	ll ans = INF;
	FOR(i,1,N){
		ans = min(ans,minf[i-1]+minr[i]);
	}
	cout << ans << endl;
}

0