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

ABC257-C Robot Takahashi


問題へのリンク


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("5");
            WillReturn.Add("10101");
            WillReturn.Add("60 45 30 40 80");
            //4
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3");
            WillReturn.Add("000");
            WillReturn.Add("1 2 3");
            //3
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("5");
            WillReturn.Add("10101");
            WillReturn.Add("60 50 50 50 60");
            //4
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static string mS;
    static int[] mWArr;

    static void Main()
    {
        List<string> InputList = GetInputList();
        mS = InputList[1];
        mWArr = InputList[2].Split(' ').Select(pX => int.Parse(pX)).ToArray();

        var ChildWList = new List<int>();
        var AdultWList = new List<int>();

        for (int I = 0; I <= mS.Length - 1; I++) {
            if (mS[I] == '0') ChildWList.Add(mWArr[I]);
            if (mS[I] == '1') AdultWList.Add(mWArr[I]);
        }

        ChildWList.Sort();
        AdultWList.Sort();

        var CheckSet = new HashSet<int>();
        CheckSet.UnionWith(mWArr);
        CheckSet.Add(0);
        CheckSet.Add(mWArr.Max() + 1);

        int ChildCnt = ChildWList.Count;
        int AdultCnt = AdultWList.Count;

        int Answer = 0;
        foreach (int EachInt in CheckSet) {
            int CorrectCnt = 0;
            if (ChildWList.Count > 0) {
                int LowerB1 = ExecNibunhou_LowerBound(EachInt, ChildWList);
                int WrongChild = 0;
                if (LowerB1 > -1) {
                    WrongChild = ChildWList.Count - 1 - LowerB1 + 1;
                }
                int CorrectChild = ChildCnt - WrongChild;
                CorrectCnt += CorrectChild;
            }

            if (AdultWList.Count > 0) {
                int LowerB2 = ExecNibunhou_LowerBound(EachInt, AdultWList);

                int CorrectAdult = 0;
                if (LowerB2 > -1) {
                    CorrectAdult = AdultWList.Count - 1 - LowerB2 + 1;
                }
                CorrectCnt += CorrectAdult;
            }
            Answer = Math.Max(Answer, CorrectCnt);
        }
        Console.WriteLine(Answer);
    }

    // 二分法で、Val以上で最小の値を持つ、添字を返す (List版)
    static int ExecNibunhou_LowerBound(int pVal, List<int> pList)
    {
        // 最後の要素がVal未満の特殊ケース
        if (pVal > pList.Last()) {
            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;
    }
}


解説

最適解候補の実数Xは
●0
●各体重
●体重の最大値+1
ですので
最適解候補ごとに、
大人と子供の人数を二分探索してます。