DPコンテスト    次のDPコンテストの問題へ    前のDPコンテストの問題へ

TDP K ターゲット


問題へのリンク


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");
            WillReturn.Add("1 1");
            WillReturn.Add("0 3");
            WillReturn.Add("3 2");
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2");
            WillReturn.Add("1 1");
            WillReturn.Add("2 2");
            //1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct RangeInfoDef
    {
        internal int L;
        internal int R;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        var RangeInfoList = new List<RangeInfoDef>();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);

            int X = wkArr[0];
            int R = wkArr[1];

            RangeInfoDef WillAdd;
            WillAdd.L = X - R;
            WillAdd.R = X + R;
            RangeInfoList.Add(WillAdd);
        }

        int[] ValArr = RangeInfoList.OrderByDescending(pX => pX.L).
            ThenByDescending(pX => pX.R).Select(pX => pX.R).ToArray();

        // LISの最終値の最小値[LISの長さ] なDP表
        var DPSortedList = new SortedList<int, int>();

        foreach (int EachVal in ValArr) {
            if (DPSortedList.Count == 0) {
                DPSortedList[1] = EachVal;
                continue;
            }
            int UpsertKeyInd = ExecNibunhou(DPSortedList, EachVal);

            int CurrUB = DPSortedList.Count - 1;
            var Keys = DPSortedList.Keys;

            // 更新する位置によって分岐
            if (UpsertKeyInd <= CurrUB) {
                DPSortedList[Keys[UpsertKeyInd]] = EachVal;
            }
            else {
                int PrevKey = Keys[CurrUB];
                DPSortedList[PrevKey + 1] = EachVal;
            }
        }
        Console.WriteLine(DPSortedList.Keys.Max());
    }

    // 二分法で、引数の値を設定する、キーの配列の添字を返す
    static int ExecNibunhou(SortedList<int, int> pDPSortedList, int pTargetVal)
    {
        int UB = pDPSortedList.Count - 1;
        var Keys = pDPSortedList.Keys;

        // 最小値以下の場合
        if (pTargetVal <= pDPSortedList[Keys[0]]) {
            return 0;
        }

        // 最大値より大きい場合
        if (pTargetVal > pDPSortedList[Keys[UB]]) {
            return UB + 1;
        }

        int L = 0;
        int R = UB;

        while (L + 1 < R) {
            int Mid = (L + R) / 2;
            if (pDPSortedList[Keys[Mid]] < pTargetVal) {
                L = Mid;
            }
            else {
                R = Mid;
            }
        }
        return R;
    }
}


解説

左端と右端の区間を最大でいくつ内包できるかという問題に帰着できます。

真に内包なので
OrderBy 左端 DESC , 右端 DESC
の順で、右端の座標でLIS長さを求めてます。