結果

問題 No.198 キャンディー・ボックス2
ユーザー 14番14番
提出日時 2016-07-21 03:23:15
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,598 bytes
コンパイル時間 2,954 ms
コンパイル使用メモリ 108,160 KB
実行使用メモリ 18,816 KB
最終ジャッジ日時 2024-04-23 19:23:07
合計ジャッジ時間 2,978 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
18,304 KB
testcase_01 AC 25 ms
18,688 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 25 ms
18,304 KB
testcase_10 AC 24 ms
18,560 KB
testcase_11 AC 25 ms
18,688 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 24 ms
18,432 KB
testcase_15 AC 25 ms
18,816 KB
testcase_16 AC 24 ms
18,304 KB
testcase_17 AC 26 ms
18,688 KB
testcase_18 AC 26 ms
18,560 KB
testcase_19 AC 26 ms
18,688 KB
testcase_20 AC 25 ms
18,560 KB
testcase_21 AC 25 ms
18,432 KB
testcase_22 AC 25 ms
18,560 KB
testcase_23 AC 26 ms
18,560 KB
testcase_24 WA -
testcase_25 AC 26 ms
18,432 KB
testcase_26 AC 26 ms
18,816 KB
testcase_27 WA -
testcase_28 AC 26 ms
18,560 KB
testcase_29 AC 26 ms
18,816 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(q1 == minCount) {
            return this.GetAns(min, q2);
        } else if(q2 == minCount) {
            return this.GetAns(q1, q3);
        } else if(q3 == 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 = @"

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