結果

問題 No.198 キャンディー・ボックス2
ユーザー nanophoto12nanophoto12
提出日時 2015-12-13 23:16:52
言語 C#(csc)
(csc 3.9.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,569 bytes
コンパイル時間 3,917 ms
コンパイル使用メモリ 112,684 KB
実行使用メモリ 26,956 KB
最終ジャッジ日時 2024-05-01 15:37:24
合計ジャッジ時間 2,656 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
24,600 KB
testcase_01 AC 24 ms
24,856 KB
testcase_02 AC 25 ms
24,628 KB
testcase_03 AC 22 ms
24,852 KB
testcase_04 AC 22 ms
24,852 KB
testcase_05 AC 24 ms
26,588 KB
testcase_06 AC 24 ms
26,824 KB
testcase_07 AC 23 ms
26,956 KB
testcase_08 AC 23 ms
24,604 KB
testcase_09 AC 24 ms
26,704 KB
testcase_10 AC 22 ms
24,856 KB
testcase_11 AC 22 ms
24,724 KB
testcase_12 AC 24 ms
24,752 KB
testcase_13 WA -
testcase_14 AC 25 ms
24,496 KB
testcase_15 AC 25 ms
22,836 KB
testcase_16 AC 23 ms
22,840 KB
testcase_17 AC 24 ms
24,792 KB
testcase_18 AC 24 ms
24,468 KB
testcase_19 AC 24 ms
24,784 KB
testcase_20 AC 26 ms
22,960 KB
testcase_21 AC 27 ms
24,756 KB
testcase_22 AC 26 ms
24,920 KB
testcase_23 AC 25 ms
24,748 KB
testcase_24 AC 26 ms
24,780 KB
testcase_25 AC 27 ms
26,708 KB
testcase_26 AC 25 ms
24,672 KB
testcase_27 AC 27 ms
26,844 KB
testcase_28 AC 26 ms
24,728 KB
testcase_29 AC 25 ms
24,492 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;

namespace No198
{
	class MainClass
	{
		private static long Calculate(long target, long[] values)
		{
			return values.Sum (v => Math.Abs (target - v));
		}

		public static void Main (string[] args)
		{			
			var b = Convert.ToInt64(Console.ReadLine ());
			var n = Convert.ToInt32 (Console.ReadLine ());
			long[] data = new long[n];
			for (int i = 0; i < n; i++) {
				data [i] = Convert.ToInt64 (Console.ReadLine ());
			}
			var sum = data.Sum () + b;
			var maxValue = sum / n;
			var minValue = data.Min ();
			var midValue = (maxValue + minValue) / 2L;

			var min = long.MaxValue;

			do {
				var maxOperation = Calculate (maxValue, data);
				var minOperation = Calculate (minValue, data);
				var midOperation = Calculate (midValue, data);
				min = Math.Min (min, maxOperation);
				min = Math.Min (min, minOperation);
				min = Math.Min (min, midOperation);

				if (maxOperation > minOperation) {
					maxValue = (midValue + maxValue) / 2;
					midValue = (maxValue + minValue) / 2;
				} else {
					minValue = (midValue + minValue) / 2;
					midValue = (maxValue + minValue) / 2;
				}
			} while (maxValue - midValue > 1 && midValue - minValue > 1);
			{
				var maxOperation = Calculate (maxValue, data);
				var minOperation = Calculate (minValue, data);
				var midOperation = Calculate (midValue, data);
				min = Math.Min (min, maxOperation);
				min = Math.Min (min, minOperation);
				min = Math.Min (min, midOperation);
			}
			Console.WriteLine (min.ToString ());
		}
	}
}
0