AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC174-E Logs


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("2 3");
            WillReturn.Add("7 9");
            //4
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 0");
            WillReturn.Add("3 4 5");
            //5
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10 10");
            WillReturn.Add("158260522 877914575 602436426 24979445 861648772 623690081 433933447 476190629 262703497 211047202");
            //292638192
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long mK;
    static long[] mAArr;

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        mK = wkArr[1];

        mAArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();

        long L = 0;
        long R = mAArr.Max();

        while (L + 1 < R) {
            long Mid = (L + R) / 2;

            if (CanAchieve(Mid)) {
                R = Mid;
            }
            else {
                L = Mid;
            }
        }
        Console.WriteLine(R);
    }

    // 全ての丸太の長さを、pLen以下にできるか
    static bool CanAchieve(long pLen)
    {
        if (pLen == 0) return false;

        long RestK = mK;
        foreach (long EachLong in mAArr) {
            if (EachLong <= pLen) {
                continue;
            }
            RestK -= (EachLong - 1) / pLen;
            if (RestK < 0) return false;
        }
        return true;
    }
}


解説

判定関数を用意して、答えを二分探索してます。