結果

問題 No.198 キャンディー・ボックス2
ユーザー 14番14番
提出日時 2016-05-03 12:04:02
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,850 bytes
コンパイル時間 1,170 ms
コンパイル使用メモリ 110,080 KB
実行使用メモリ 19,328 KB
最終ジャッジ日時 2024-04-15 09:55:49
合計ジャッジ時間 2,905 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
19,072 KB
testcase_01 AC 32 ms
18,944 KB
testcase_02 AC 32 ms
19,072 KB
testcase_03 AC 32 ms
18,688 KB
testcase_04 AC 31 ms
19,328 KB
testcase_05 AC 32 ms
18,944 KB
testcase_06 AC 31 ms
18,944 KB
testcase_07 AC 32 ms
19,200 KB
testcase_08 AC 34 ms
19,200 KB
testcase_09 AC 32 ms
19,072 KB
testcase_10 AC 31 ms
18,688 KB
testcase_11 AC 31 ms
18,944 KB
testcase_12 AC 31 ms
19,200 KB
testcase_13 AC 31 ms
19,072 KB
testcase_14 AC 31 ms
19,072 KB
testcase_15 AC 32 ms
18,944 KB
testcase_16 AC 31 ms
18,944 KB
testcase_17 AC 31 ms
19,200 KB
testcase_18 AC 32 ms
18,944 KB
testcase_19 AC 31 ms
18,688 KB
testcase_20 AC 31 ms
18,944 KB
testcase_21 AC 31 ms
18,688 KB
testcase_22 AC 32 ms
19,072 KB
testcase_23 WA -
testcase_24 AC 32 ms
18,816 KB
testcase_25 WA -
testcase_26 AC 32 ms
19,072 KB
testcase_27 AC 32 ms
19,072 KB
testcase_28 WA -
testcase_29 AC 31 ms
18,944 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.Text;
using System.Linq;
 
class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        long temotiCount = long.Parse(Reader.ReadLine());
        int boxCount = int.Parse(Reader.ReadLine());
        
        List<long> boxList = new List<long>();
        Dictionary<long, int> dic = new Dictionary<long, int>();
        long total = 0;
        for(int i=0; i<boxCount; i++) {
            long val = long.Parse(Reader.ReadLine());
            if(dic.ContainsKey(val)) {
                dic[val]++;
            } else
            {
                dic.Add(val, 1);
            }
            boxList.Add(val);
            total += val;
        }
        long heikin = total / boxCount;
        if(Math.Abs(total - (heikin + 1) * boxCount) < Math.Abs(total - heikin * boxCount)) {
            heikin++;
        }
        while ((heikin * boxCount - total) > temotiCount)
        {
            heikin--;
        }
        // 最頻値
        List<long> modeList = new List<long>();
        int prevCnt = dic.Max(a=>a.Value);
        dic.ToList().ForEach((a)=>{
           if(a.Value == prevCnt && total + temotiCount >= a.Key * boxCount) {
               modeList.Add(a.Key);
           } 
        });
        
        long ans = long.MaxValue;
        long tmp = 0;
        boxList.ForEach((a)=>{
            tmp += Math.Abs(a - heikin);
        });
        ans = Math.Min(ans, tmp);
        modeList.ForEach((a)=>{
            tmp = 0;
            boxList.ForEach((b)=>{
                tmp+=Math.Abs(a-b);
            });
            ans = Math.Min(ans, tmp);
        });
        Console.WriteLine(ans);

    }
    



    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


0
2
0
1


 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        public static int[] GetInt(char delimiter = ' ', bool trim = false)
        {
            string inptStr = ReadLine();
            if (trim)
            {
                inptStr = inptStr.Trim();
            }
            string[] inpt = inptStr.Split(delimiter);
            int[] ret = new int[inpt.Length];
            for (int i = 0; i < inpt.Length; i++)
            {
                ret[i] = int.Parse(inpt[i]);
            }
            return ret;
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0