結果

問題 No.198 キャンディー・ボックス2
ユーザー nanophoto12nanophoto12
提出日時 2015-12-13 22:58:24
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,225 bytes
コンパイル時間 2,249 ms
コンパイル使用メモリ 105,344 KB
実行使用メモリ 23,812 KB
最終ジャッジ日時 2023-10-13 15:03:00
合計ジャッジ時間 4,831 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
23,628 KB
testcase_01 AC 57 ms
21,716 KB
testcase_02 WA -
testcase_03 AC 56 ms
23,596 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 55 ms
21,572 KB
testcase_07 WA -
testcase_08 AC 54 ms
21,476 KB
testcase_09 AC 53 ms
21,680 KB
testcase_10 WA -
testcase_11 AC 57 ms
21,556 KB
testcase_12 AC 55 ms
23,668 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 54 ms
21,560 KB
testcase_16 WA -
testcase_17 AC 59 ms
23,580 KB
testcase_18 AC 55 ms
21,572 KB
testcase_19 WA -
testcase_20 AC 54 ms
21,476 KB
testcase_21 WA -
testcase_22 AC 57 ms
21,656 KB
testcase_23 WA -
testcase_24 AC 58 ms
23,632 KB
testcase_25 WA -
testcase_26 AC 56 ms
21,604 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 55 ms
19,624 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 + 1;
			var minValue = data.Min ();
			var midValue = (maxValue + minValue) / 2L;

			var min = long.MaxValue;

			while (maxValue - 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);

				if (maxOperation > minOperation) {
					maxValue = midValue;
					midValue = (maxValue + minValue) / 2;
				} else {
					minValue = midValue;
					midValue = (maxValue + minValue) / 2;
				}
			}
			Console.WriteLine (min.ToString ());
		}
	}
}
0