結果

問題 No.198 キャンディー・ボックス2
ユーザー nanophoto12nanophoto12
提出日時 2015-12-13 23:16:52
言語 C#(csc)
(csc 3.9.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,569 bytes
コンパイル時間 2,168 ms
コンパイル使用メモリ 105,228 KB
実行使用メモリ 23,732 KB
最終ジャッジ日時 2023-08-14 02:40:53
合計ジャッジ時間 5,536 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
21,664 KB
testcase_01 AC 58 ms
21,616 KB
testcase_02 AC 57 ms
21,564 KB
testcase_03 AC 58 ms
21,756 KB
testcase_04 AC 58 ms
21,592 KB
testcase_05 AC 58 ms
23,548 KB
testcase_06 AC 59 ms
21,560 KB
testcase_07 AC 58 ms
19,524 KB
testcase_08 AC 59 ms
21,564 KB
testcase_09 AC 59 ms
21,656 KB
testcase_10 AC 59 ms
23,608 KB
testcase_11 AC 58 ms
21,724 KB
testcase_12 AC 58 ms
21,628 KB
testcase_13 WA -
testcase_14 AC 58 ms
21,680 KB
testcase_15 AC 59 ms
21,644 KB
testcase_16 AC 58 ms
23,732 KB
testcase_17 AC 59 ms
23,680 KB
testcase_18 AC 59 ms
21,604 KB
testcase_19 AC 58 ms
21,560 KB
testcase_20 AC 58 ms
23,620 KB
testcase_21 AC 58 ms
23,684 KB
testcase_22 AC 58 ms
21,620 KB
testcase_23 AC 58 ms
21,576 KB
testcase_24 AC 57 ms
21,696 KB
testcase_25 AC 58 ms
23,560 KB
testcase_26 AC 59 ms
23,712 KB
testcase_27 AC 59 ms
21,732 KB
testcase_28 AC 59 ms
21,632 KB
testcase_29 AC 58 ms
21,664 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