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

ABC294-F Sugar Water 2


問題へのリンク


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("3 1 1");
            WillReturn.Add("1 2");
            WillReturn.Add("4 1");
            WillReturn.Add("1 4");
            WillReturn.Add("1 4");
            //50.000000000000000
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 2 2");
            WillReturn.Add("6 4");
            WillReturn.Add("10 1");
            WillReturn.Add("5 8");
            WillReturn.Add("9 6");
            //62.500000000000000
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("4 5 10");
            WillReturn.Add("5 4");
            WillReturn.Add("1 6");
            WillReturn.Add("7 4");
            WillReturn.Add("9 8");
            WillReturn.Add("2 2");
            WillReturn.Add("5 6");
            WillReturn.Add("6 7");
            WillReturn.Add("5 3");
            WillReturn.Add("8 1");
            //54.166666666666664
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long[] GetSplitArr(string pStr)
    {
        return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
    }

    struct WaterDef
    {
        internal double Sugar;
        internal double Water;
    }
    static List<WaterDef> WaterList1 = new List<WaterDef>();
    static List<WaterDef> WaterList2 = new List<WaterDef>();

    static long mK;

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] wkArr = GetSplitArr(InputList[0]);
        long N = wkArr[0];
        mK = wkArr[2];

        Action<string> SplitAct = (pStr) => wkArr = GetSplitArr(pStr);
        foreach (string EachStr in InputList.Skip(1).Take((int)N)) {
            SplitAct(EachStr);
            WaterDef WillAdd;
            WillAdd.Water = (double)wkArr[0];
            WillAdd.Sugar = (double)wkArr[1];
            WaterList1.Add(WillAdd);
        }

        foreach (string EachStr in InputList.Skip(1 + (int)N)) {
            SplitAct(EachStr);
            WaterDef WillAdd;
            WillAdd.Water = (double)wkArr[0];
            WillAdd.Sugar = (double)wkArr[1];
            WaterList2.Add(WillAdd);
        }

        // 解がX以上ですか?
        double L = 0;
        double R = 100;

        while (L + 0.0000000001 < R) {
            double Mid = (L + R) / 2;

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

    }

    // 解がX以上ですか?
    static bool CanAchieve(double pX)
    {
        double R = pX / 100;

        var UhenList = new List<double>();
        foreach (WaterDef EachWater in WaterList2) {
            double Uhen = R * (EachWater.Water + EachWater.Sugar) - EachWater.Water;
            UhenList.Add(Uhen);
        }
        UhenList.Sort();

        long NeedCnt = mK;
        foreach (WaterDef EachWater in WaterList1) {
            double Sahen = -R * (EachWater.Water + EachWater.Sugar) + EachWater.Water;
            long OkCnt = GetListRangeValueCnt.GetLessOrEqualCnt(UhenList, Sahen);
            NeedCnt -= OkCnt;
        }
        return NeedCnt <= 0;
    }
}

#region GetListRangeValueCnt
// {昇順にソートされたList,Min,Max}を引数とし、
// Min以上Max以下な値の個数を返す
internal class GetListRangeValueCnt
{
    // 機能01 Min以上な値の個数を返す
    static internal long GetMoreOrEqualCnt(List<double> pSortedList, double pMinVal)
    {
        int ResultInd = ExecNibunhou_LowerBound(pMinVal, pSortedList);

        if (ResultInd == -1) return 0;
        return (pSortedList.Count - 1) - ResultInd + 1;
    }

    // 機能02 Min超えな値の個数を返す
    static internal long GetMoreStrictCnt(List<double> pSortedList, double pMinVal)
    {
        int ResultInd = ExecNibunhou_UpperBound(pMinVal, pSortedList);

        if (ResultInd == -1) return 0;
        return (pSortedList.Count - 1) - ResultInd + 1;
    }

    // 機能03 Max以下な値の個数を返す
    static internal long GetLessOrEqualCnt(List<double> pSortedList, double pMaxVal)
    {
        int ResultInd = ExecNibunhou_LowerOrEqual_Max(pMaxVal, pSortedList);

        if (ResultInd == -1) return 0;
        return ResultInd + 1;
    }

    // 機能04 Max未満な値の個数を返す
    static internal long GetLessStrictCnt(List<double> pSortedList, double pMaxVal)
    {
        int ResultInd = ExecNibunhou_LowerMax(pMaxVal, pSortedList);

        if (ResultInd == -1) return 0;
        return ResultInd + 1;
    }

    // 機能05 Min以上Max以下な値の個数を返す
    static internal long GetRangeCnt(List<double> pSortedList, double pMinVal, double pMaxVal)
    {
        if (pMinVal > pMaxVal) {
            throw new Exception("pMinVal > pMaxVal");
        }

        int ResultInd1 = ExecNibunhou_LowerBound(pMinVal, pSortedList);
        if (ResultInd1 == -1) return 0;

        int ResultInd2 = ExecNibunhou_LowerOrEqual_Max(pMaxVal, pSortedList);
        if (ResultInd2 == -1) return 0;

        return ResultInd2 - ResultInd1 + 1;
    }

    // 二分法で、Val以上で最小の値を持つ、添字を返す
    static private int ExecNibunhou_LowerBound(double pVal, List<double> pList)
    {
        if (pList.Count == 0) return -1;

        // 最後の要素がVal未満の特殊ケース
        if (pVal > pList[pList.Count - 1]) {
            return -1;
        }
        // 最初の要素がVal以上の特殊ケース
        if (pVal <= pList[0]) {
            return 0;
        }

        int L = 0;
        int R = pList.Count - 1;

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

            if (pList[Mid] >= pVal) {
                R = Mid;
            }
            else {
                L = Mid;
            }
        }
        return R;
    }

    // 二分法で、Val超えで最小の値を持つ、添字を返す
    static private int ExecNibunhou_UpperBound(double pVal, List<double> pList)
    {
        // 要素が0件のケース
        if (pList.Count == 0) return -1;

        // 最後の要素がVal以下の特殊ケース
        if (pVal >= pList[pList.Count - 1]) {
            return -1;
        }
        // 最初の要素がVal超えの特殊ケース
        if (pVal < pList[0]) {
            return 0;
        }

        int L = 0;
        int R = pList.Count - 1;

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

            if (pList[Mid] > pVal) {
                R = Mid;
            }
            else {
                L = Mid;
            }
        }
        return R;
    }

    // 二分法で、Val以下で最大の値を持つ、添字を返す
    static private int ExecNibunhou_LowerOrEqual_Max(double pVal, List<double> pList)
    {
        if (pList.Count == 0) return -1;

        // 最後の要素がVal以下の特殊ケース
        if (pVal >= pList[pList.Count - 1]) {
            return pList.Count - 1;
        }
        // 最初の要素がVal超えの特殊ケース
        if (pVal < pList[0]) {
            return -1;
        }

        int L = 0;
        int R = pList.Count - 1;

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

            if (pList[Mid] <= pVal) {
                L = Mid;
            }
            else {
                R = Mid;
            }
        }
        return L;
    }

    // 二分法で、Val未満で最大の値を持つ、添字を返す
    static private int ExecNibunhou_LowerMax(double pVal, List<double> pList)
    {
        if (pList.Count == 0) return -1;

        // 最後の要素がVal未満の特殊ケース
        if (pVal > pList[pList.Count - 1]) {
            return pList.Count - 1;
        }
        // 最初の要素がVal以上の特殊ケース
        if (pVal <= pList[0]) {
            return -1;
        }

        int L = 0;
        int R = pList.Count - 1;

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

            if (pList[Mid] < pVal) {
                L = Mid;
            }
            else {
                R = Mid;
            }
        }
        return L;
    }
}
#endregion


解説

濃度のK番目を求めるので
解が25%以上ですか?
という部分問題を高速に解くことを考えます。

「解が25%以上ですか?」と
「濃度25%以上の組合せがK個以上存在しますか?」
は、同値です。

Aグラムの砂糖とBグラムの水
Cグラムの砂糖とDグラムの水
を混ぜて、濃度が25%以上であることは
(A+C) / (A+B+C+D) >= 0.25
A+C >= 0.25A + 0.25B + 0.25C + 0.25D
AとBを右辺、CとDを左辺に移項すると
0.75A - 0.25B >= -0.75C + 0.25D

なので、右辺だけを前計算したListをソートしておき
0.75A - 0.25Bの結果ごとに、Listの二分探索で、
高速に個数を求めることが可能です。