結果
| 問題 | 
                            No.837 Noelちゃんと星々2
                             | 
                    
| コンテスト | |
| ユーザー | 
                             Risen
                         | 
                    
| 提出日時 | 2019-06-15 11:17:35 | 
| 言語 | C#(csc)  (csc 3.9.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 117 ms / 2,000 ms | 
| コード長 | 1,313 bytes | 
| コンパイル時間 | 2,388 ms | 
| コンパイル使用メモリ | 107,392 KB | 
| 実行使用メモリ | 37,120 KB | 
| 最終ジャッジ日時 | 2024-06-11 14:21:58 | 
| 合計ジャッジ時間 | 3,962 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 29 | 
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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);
    }
}
            
            
            
        
            
Risen