結果

問題 No.838 Noelちゃんと星々3
ユーザー leaf_1415leaf_1415
提出日時 2019-06-14 21:45:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 643 bytes
コンパイル時間 1,128 ms
コンパイル使用メモリ 62,668 KB
実行使用メモリ 5,840 KB
最終ジャッジ日時 2023-08-08 17:52:55
合計ジャッジ時間 2,094 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
5,840 KB
testcase_01 AC 50 ms
5,772 KB
testcase_02 AC 50 ms
5,712 KB
testcase_03 AC 51 ms
5,712 KB
testcase_04 AC 50 ms
5,708 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#define llint long long

using namespace std;

llint n;
llint x[100005], sum[100005];
llint dp[100005];

llint calc(llint l, llint r)
{
	llint m = (l+r)/2;
	return (m-(l-1))*x[m] - (sum[m]-sum[l-1]) + (sum[r]-sum[m]) - (r-m)*x[m];
}

int main(void)
{
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> x[i];
	sort(x+1, x+n+1);
	for(int i = 1; i <= n; i++) sum[i] = sum[i-1] + x[i];

	for(int i = 0; i <= n; i++) dp[i] = 1e18;
	dp[0] = 0;
	for(int i = 1; i <= n; i++){
		for(int j = 2; j <= 3; j++){
			if(i-j >= 0) dp[i] = min(dp[i], dp[i-j] + calc(i-j+1, i));
		}
	}
	cout << dp[n] << endl;
	
	return 0;
}
0