結果

問題 No.837 Noelちゃんと星々2
ユーザー startcppstartcpp
提出日時 2019-06-14 22:38:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 729 bytes
コンパイル時間 606 ms
コンパイル使用メモリ 62,484 KB
実行使用メモリ 5,148 KB
最終ジャッジ日時 2023-09-02 07:21:34
合計ジャッジ時間 2,534 ms
ジャッジサーバーID
(参考情報)
judge13 / judge16
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
4,916 KB
testcase_01 AC 49 ms
5,148 KB
testcase_02 AC 49 ms
4,928 KB
testcase_03 AC 50 ms
4,932 KB
testcase_04 AC 50 ms
4,960 KB
testcase_05 AC 50 ms
4,980 KB
testcase_06 AC 50 ms
4,912 KB
testcase_07 AC 49 ms
4,908 KB
testcase_08 AC 50 ms
5,140 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,388 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//→.←|→.←と動く. |を全探索する. 累積和で殴る. O(N).
#include <iostream>
#include <algorithm>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int n;
int x[100000];
int rx[100001];

int getCost(int l, int r) {
	int n = r - l;
	int mid = n / 2 + l;
	
	int right = rx[r] - rx[mid + 1];
	int left = rx[mid] - rx[l];
	return right - left + x[mid] * (n % 2 == 0);
}

signed main() {
	int i;
	
	cin >> n;
	rep(i, n) cin >> x[i];
	sort(x, x + n);
	if (x[0] == x[n - 1]) { cout << 1 << endl; return 0; }
	
	rep(i, n) rx[i + 1] = rx[i] + x[i];
	
	int ans = 1e+18;
	rep(i, n) {
		int res = getCost(0, i) + getCost(i, n);
		ans = min(ans, res);
	}
	cout << ans << endl;
	return 0;
}
0