結果

問題 No.198 キャンディー・ボックス2
ユーザー 14番14番
提出日時 2016-07-21 03:26:33
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 27 ms / 1,000 ms
コード長 2,625 bytes
コンパイル時間 1,055 ms
コンパイル使用メモリ 117,480 KB
実行使用メモリ 28,744 KB
最終ジャッジ日時 2024-04-28 01:56:31
合計ジャッジ時間 2,741 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
24,560 KB
testcase_01 AC 26 ms
24,624 KB
testcase_02 AC 27 ms
26,572 KB
testcase_03 AC 26 ms
24,440 KB
testcase_04 AC 27 ms
26,608 KB
testcase_05 AC 27 ms
24,788 KB
testcase_06 AC 26 ms
24,696 KB
testcase_07 AC 27 ms
28,744 KB
testcase_08 AC 27 ms
25,044 KB
testcase_09 AC 26 ms
24,564 KB
testcase_10 AC 25 ms
26,404 KB
testcase_11 AC 27 ms
24,696 KB
testcase_12 AC 27 ms
24,668 KB
testcase_13 AC 26 ms
24,696 KB
testcase_14 AC 26 ms
24,496 KB
testcase_15 AC 26 ms
26,828 KB
testcase_16 AC 26 ms
26,484 KB
testcase_17 AC 26 ms
26,832 KB
testcase_18 AC 26 ms
24,652 KB
testcase_19 AC 26 ms
22,704 KB
testcase_20 AC 27 ms
26,700 KB
testcase_21 AC 26 ms
26,828 KB
testcase_22 AC 26 ms
24,492 KB
testcase_23 AC 27 ms
22,456 KB
testcase_24 AC 26 ms
24,440 KB
testcase_25 AC 27 ms
24,916 KB
testcase_26 AC 27 ms
24,620 KB
testcase_27 AC 27 ms
24,572 KB
testcase_28 AC 26 ms
22,452 KB
testcase_29 AC 26 ms
26,732 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;

        Temoti = long.Parse(Reader.ReadLine());

        int boxCount = int.Parse(Reader.ReadLine());

        this.BoxList = new long[boxCount];
        Total = 0;
        for(int i=0; i<boxCount; i++) {
            BoxList[i] = long.Parse(Reader.ReadLine());
            Total+=BoxList[i];
        }
        long ans = this.GetAns(0, (Total + Temoti) / boxCount);
        Console.WriteLine(ans);
    }

    private long GetAns(long min, long max) {
        if(max - min <= 1) {
            return Math.Min(GetCount(min), GetCount(max));
        }

        long q2 = (max + min) / 2;
        long q1 = (q2 + min) / 2;
        long q3 = (q2 + max) / 2;

        long minVal = this.GetCount(min);
        long maxVal = this.GetCount(max);
        long q1Val = this.GetCount(q1);
        long q2Val = this.GetCount(q2);
        long q3Val = this.GetCount(q3);

        long minCount = new long[] {minVal, maxVal, q1Val, q2Val, q3Val}.Min(a=>a);
        if(minVal == minCount) {
            return this.GetAns(min, q1);
        } else if(q1Val == minCount) {
            return this.GetAns(min, q2);
        } else if(q2Val == minCount) {
            return this.GetAns(q1, q3);
        } else if(q3Val == minCount) {
            return this.GetAns(q2,max);
        } else {
            return this.GetAns(q3, max);
        }
    }
    private long GetCount(long target) {
        if(target * BoxList.Length > Temoti + Total) {
            return long.MaxValue;
        }
        long ans = 0;
        foreach (long candy in BoxList)
        {
            ans += Math.Abs(candy - target);
        }
        return ans;
    }
    private long[] BoxList;

    private long Temoti;

    private long Total;



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

169219876
10
135
24876433
24876476
24876781
24876007
2000
24875934
24876438
24876122
24876474


";
        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();
            }
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0