結果

問題 No.198 キャンディー・ボックス2
ユーザー 14番14番
提出日時 2016-05-03 12:01:39
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 2,866 bytes
コンパイル時間 1,180 ms
コンパイル使用メモリ 113,960 KB
実行使用メモリ 30,420 KB
最終ジャッジ日時 2024-04-15 09:55:45
合計ジャッジ時間 4,633 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
24,320 KB
testcase_01 AC 34 ms
18,816 KB
testcase_02 AC 35 ms
18,816 KB
testcase_03 AC 36 ms
18,944 KB
testcase_04 AC 36 ms
19,072 KB
testcase_05 AC 36 ms
19,200 KB
testcase_06 AC 34 ms
19,072 KB
testcase_07 AC 35 ms
19,072 KB
testcase_08 AC 36 ms
19,328 KB
testcase_09 AC 35 ms
18,816 KB
testcase_10 AC 37 ms
18,816 KB
testcase_11 AC 37 ms
18,944 KB
testcase_12 AC 32 ms
19,072 KB
testcase_13 AC 37 ms
19,072 KB
testcase_14 AC 31 ms
18,688 KB
testcase_15 AC 34 ms
18,816 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 ((total - heikin * boxCount) > 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 = @"


1000000000
1
1000000000


 
";
        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