結果

問題 No.837 Noelちゃんと星々2
ユーザー RisenRisen
提出日時 2019-06-15 11:17:35
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 1,313 bytes
コンパイル時間 5,103 ms
コンパイル使用メモリ 106,108 KB
実行使用メモリ 43,496 KB
最終ジャッジ日時 2023-09-02 07:24:55
合計ジャッジ時間 7,124 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 166 ms
41,500 KB
testcase_01 AC 165 ms
39,392 KB
testcase_02 AC 166 ms
41,512 KB
testcase_03 AC 164 ms
41,380 KB
testcase_04 AC 166 ms
41,496 KB
testcase_05 AC 167 ms
41,660 KB
testcase_06 AC 167 ms
41,616 KB
testcase_07 AC 166 ms
43,496 KB
testcase_08 AC 165 ms
41,412 KB
testcase_09 AC 67 ms
22,028 KB
testcase_10 AC 66 ms
21,932 KB
testcase_11 AC 66 ms
21,944 KB
testcase_12 AC 67 ms
21,964 KB
testcase_13 AC 68 ms
21,916 KB
testcase_14 AC 65 ms
22,108 KB
testcase_15 AC 65 ms
21,916 KB
testcase_16 AC 65 ms
22,028 KB
testcase_17 AC 66 ms
23,936 KB
testcase_18 AC 66 ms
23,896 KB
testcase_19 AC 67 ms
21,960 KB
testcase_20 AC 65 ms
21,924 KB
testcase_21 AC 65 ms
22,044 KB
testcase_22 AC 67 ms
23,900 KB
testcase_23 AC 68 ms
19,816 KB
testcase_24 AC 67 ms
21,924 KB
testcase_25 AC 66 ms
23,940 KB
testcase_26 AC 67 ms
22,100 KB
testcase_27 AC 65 ms
21,820 KB
testcase_28 AC 66 ms
23,960 KB
testcase_29 AC 66 ms
24,012 KB
testcase_30 AC 65 ms
21,904 KB
testcase_31 AC 65 ms
21,936 KB
testcase_32 AC 66 ms
22,100 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

public class Solution
{
    public static void Main()
    {
        var N = int.Parse(Console.ReadLine());
        var Y = Console.ReadLine().Split(' ').Select(long.Parse).OrderBy(i => i).ToArray();

        if (Y.All(y => y == Y[0]))
        {
            Console.WriteLine(1);
            return;
        }

        // 距離の和 = Σ|y - 中央値|
        // 中央値前後で符号が逆転することにより絶対値を外し,中央値を消す

        var acc = new long[N + 1];
        for (int i = 0; i < N; i++)
        {
            acc[i + 1] = Y[i] + acc[i];
        }

        // [start, end)
        long getDiff(int start, int end)
        {
            if (start + 1 == end)
            {
                return 0;
            }
            var mid = (end - start) / 2 + start;
            if ((end - start) % 2 == 0)
            {
                return acc[end] - acc[mid] - (acc[mid] - acc[start]);
            }
            return acc[end] - acc[mid + 1] - (acc[mid] - acc[start]);
        }

        var min = long.MaxValue;
        for (int k = 1; k < N; k++)
        {
            var diff = getDiff(0, k) + getDiff(k, N);
            min = Math.Min(diff, min);
        }

        Console.WriteLine(min);
    }
}
0