結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
21,616 KB
testcase_01 AC 55 ms
23,728 KB
testcase_02 WA -
testcase_03 AC 54 ms
21,520 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 53 ms
23,608 KB
testcase_07 WA -
testcase_08 AC 55 ms
21,656 KB
testcase_09 AC 53 ms
21,524 KB
testcase_10 AC 54 ms
21,536 KB
testcase_11 AC 53 ms
23,624 KB
testcase_12 AC 55 ms
23,568 KB
testcase_13 WA -
testcase_14 AC 55 ms
23,556 KB
testcase_15 AC 54 ms
21,500 KB
testcase_16 AC 54 ms
19,484 KB
testcase_17 AC 56 ms
21,532 KB
testcase_18 AC 55 ms
21,556 KB
testcase_19 AC 54 ms
21,528 KB
testcase_20 AC 54 ms
21,568 KB
testcase_21 AC 54 ms
21,568 KB
testcase_22 AC 56 ms
21,520 KB
testcase_23 AC 56 ms
23,556 KB
testcase_24 AC 56 ms
23,472 KB
testcase_25 AC 53 ms
19,508 KB
testcase_26 AC 53 ms
23,600 KB
testcase_27 WA -
testcase_28 AC 54 ms
21,624 KB
testcase_29 AC 53 ms
21,680 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;
					midValue = (maxValue + minValue) / 2;
				} else {
					minValue = midValue;
					midValue = (maxValue + minValue) / 2;
				}
			} while (maxValue - minValue > 1);
			Console.WriteLine (min.ToString ());
		}
	}
}
0