結果

問題 No.198 キャンディー・ボックス2
ユーザー nanophoto12nanophoto12
提出日時 2015-05-05 00:29:46
言語 C#(csc)
(csc 3.9.0)
結果
RE  
実行時間 -
コード長 2,037 bytes
コンパイル時間 3,133 ms
コンパイル使用メモリ 106,816 KB
実行使用メモリ 23,804 KB
最終ジャッジ日時 2023-09-19 06:56:54
合計ジャッジ時間 6,344 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
19,688 KB
testcase_01 AC 60 ms
21,676 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 WA -
testcase_09 AC 61 ms
19,492 KB
testcase_10 AC 59 ms
21,732 KB
testcase_11 AC 60 ms
21,832 KB
testcase_12 AC 64 ms
23,776 KB
testcase_13 RE -
testcase_14 AC 63 ms
21,656 KB
testcase_15 AC 62 ms
21,624 KB
testcase_16 AC 60 ms
19,564 KB
testcase_17 AC 60 ms
21,832 KB
testcase_18 AC 60 ms
23,732 KB
testcase_19 AC 60 ms
21,592 KB
testcase_20 AC 60 ms
21,780 KB
testcase_21 AC 61 ms
21,628 KB
testcase_22 AC 59 ms
23,704 KB
testcase_23 AC 60 ms
23,584 KB
testcase_24 RE -
testcase_25 AC 59 ms
21,668 KB
testcase_26 AC 60 ms
23,804 KB
testcase_27 WA -
testcase_28 AC 60 ms
21,644 KB
testcase_29 AC 60 ms
19,588 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.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