結果

問題 No.198 キャンディー・ボックス2
ユーザー nanophoto12
提出日時 2015-05-05 00:29:46
言語 C#(csc)
(csc 3.9.0)
結果
RE  
実行時間 -
コード長 2,037 bytes
コンパイル時間 786 ms
コンパイル使用メモリ 113,896 KB
実行使用メモリ 28,760 KB
最終ジャッジ日時 2024-07-05 19:03:09
合計ジャッジ時間 2,442 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16 WA * 2 RE * 8
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace No198
{
    class Program
    {
        static void Main(string[] args)
        {
            var hand = long.Parse(Console.ReadLine());
            var n = long.Parse(Console.ReadLine());
            var data = new long[n];
            for (int i = 0; i < n; i++)
            {
                data[i] = long.Parse(Console.ReadLine());
            }
            var sum = hand + data.Sum();
            long minValue = 0;
            long maxValue = sum/n;
            while (maxValue - minValue > 1)
            {
                var minOperation = Calculate(data, minValue);
                var middleValue = (minValue + maxValue) / 2;
                var middleOperation = Calculate(data, middleValue);
                var maxOperation = Calculate(data, maxValue);
                var currentMin = new[] {minOperation, middleOperation, maxOperation}.Min();
                if (currentMin == middleOperation)
                {
                    if (minOperation < maxOperation)
                    {
                        maxValue = middleValue;
                    }
                    else
                    {
                        minValue = middleValue;
                    }
                }
                else if(currentMin == maxOperation)
                {
                    minValue = middleValue;
                }
                else
                {
                    maxValue = middleOperation;
                }
            }
            var result = Math.Min(Calculate(data, minValue), Calculate(data, maxValue));
            Console.WriteLine(result);
        }

        private static long Calculate(long[] data, long lastValue)
        {
            var operations = data.Select(element => Math.Abs(element - lastValue)).Sum();//+ Math.Abs(hand - lastHand);
            return operations;
        }
    }
}
0