結果

問題 No.198 キャンディー・ボックス2
ユーザー 14番14番
提出日時 2016-07-21 03:26:33
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 61 ms / 1,000 ms
コード長 2,625 bytes
コンパイル時間 2,517 ms
コンパイル使用メモリ 106,656 KB
実行使用メモリ 23,700 KB
最終ジャッジ日時 2023-08-10 08:25:34
合計ジャッジ時間 5,570 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
21,552 KB
testcase_01 AC 57 ms
21,504 KB
testcase_02 AC 56 ms
21,608 KB
testcase_03 AC 56 ms
21,484 KB
testcase_04 AC 57 ms
21,528 KB
testcase_05 AC 59 ms
21,664 KB
testcase_06 AC 61 ms
23,616 KB
testcase_07 AC 59 ms
23,592 KB
testcase_08 AC 58 ms
23,572 KB
testcase_09 AC 58 ms
21,588 KB
testcase_10 AC 57 ms
21,548 KB
testcase_11 AC 60 ms
21,528 KB
testcase_12 AC 58 ms
23,572 KB
testcase_13 AC 57 ms
21,580 KB
testcase_14 AC 58 ms
21,472 KB
testcase_15 AC 60 ms
21,652 KB
testcase_16 AC 59 ms
23,612 KB
testcase_17 AC 60 ms
19,468 KB
testcase_18 AC 59 ms
23,612 KB
testcase_19 AC 58 ms
21,580 KB
testcase_20 AC 59 ms
23,604 KB
testcase_21 AC 59 ms
23,468 KB
testcase_22 AC 58 ms
21,528 KB
testcase_23 AC 58 ms
19,524 KB
testcase_24 AC 59 ms
23,700 KB
testcase_25 AC 59 ms
21,592 KB
testcase_26 AC 58 ms
21,572 KB
testcase_27 AC 57 ms
21,716 KB
testcase_28 AC 57 ms
21,552 KB
testcase_29 AC 55 ms
21,456 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