結果

問題 No.198 キャンディー・ボックス2
ユーザー 14番
提出日時 2016-07-21 03:23:15
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,598 bytes
コンパイル時間 4,908 ms
コンパイル使用メモリ 107,544 KB
実行使用メモリ 18,816 KB
最終ジャッジ日時 2024-10-15 20:12:26
合計ジャッジ時間 2,805 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 16 WA * 10
権限があれば一括ダウンロードができます
コンパイルメッセージ
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