結果
問題 | No.837 Noelちゃんと星々2 |
ユーザー |
![]() |
提出日時 | 2021-03-16 11:04:41 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 285 ms / 2,000 ms |
コード長 | 1,178 bytes |
コンパイル時間 | 2,113 ms |
コンパイル使用メモリ | 77,884 KB |
実行使用メモリ | 52,584 KB |
最終ジャッジ日時 | 2024-06-11 14:30:43 |
合計ジャッジ時間 | 7,074 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 29 |
ソースコード
import java.util.*; import java.io.*; public class Main { public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] line = br.readLine().split(" ", n); int[] positions = new int[n]; for (int i = 0; i < n; i++) { positions[i] = Integer.parseInt(line[i]); } Arrays.sort(positions); if (positions[0] == positions[n - 1]) { System.out.println(1); return; } long[] sums = new long[n]; sums[0] = positions[0]; for (int i = 1; i < n; i++) { sums[i] = sums[i - 1] + positions[i]; } long min = Long.MAX_VALUE; for (int i = 1; i < n; i++) { int leftc = (i - 1) / 2; long tmp = positions[leftc] * (long)(leftc + 1) - sums[leftc]; tmp += sums[i - 1] - sums[leftc] - positions[leftc] * (long)(i - leftc - 1); int rightc = (n - 1 + i) / 2; tmp += positions[rightc] *(long)(rightc - i + 1) - (sums[rightc] - sums[i - 1]); tmp += sums[n - 1] - sums[rightc] - positions[rightc] * (long)(n - 1 - rightc); min = Math.min(min, tmp); } System.out.println(min); } }