典型90問    次の典型90問へ    前の典型90問へ

典型90問 037 Don't Leave the Spice(★5)


問題へのリンク


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("100 4");
            WillReturn.Add("30 40 120");
            WillReturn.Add("30 40 30");
            WillReturn.Add("30 40 1500");
            WillReturn.Add("30 40 40");
            //1660
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("100 4");
            WillReturn.Add("13 15 31415");
            WillReturn.Add("12 13 92653");
            WillReturn.Add("29 33 58979");
            WillReturn.Add("95 98 32384");
            //-1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("5000 5");
            WillReturn.Add("1000 1000 1000000000");
            WillReturn.Add("1000 1000 1000000000");
            WillReturn.Add("1000 1000 1000000000");
            WillReturn.Add("1000 1000 1000000000");
            WillReturn.Add("1000 1000 1000000000");
            //5000000000
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("10000 20");
            WillReturn.Add("4539 6002 485976");
            WillReturn.Add("1819 5162 457795");
            WillReturn.Add("1854 2246 487643");
            WillReturn.Add("1023 4733 393530");
            WillReturn.Add("1052 6274 289577");
            WillReturn.Add("1874 2436 167747");
            WillReturn.Add("1457 4248 452660");
            WillReturn.Add("2103 4189 174955");
            WillReturn.Add("3057 5061 319316");
            WillReturn.Add("4898 4953 394627");
            WillReturn.Add("1313 2880 154687");
            WillReturn.Add("1274 1364 259598");
            WillReturn.Add("3866 5844 233027");
            WillReturn.Add("1163 5036 386223");
            WillReturn.Add("1234 4630 155972");
            WillReturn.Add("2845 4978 442858");
            WillReturn.Add("3168 5368 171601");
            WillReturn.Add("3708 4407 394899");
            WillReturn.Add("3924 4122 428313");
            WillReturn.Add("2112 4169 441976");
            //2727026
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct LRWInfoDef
    {
        internal long L;
        internal long R;
        internal long W;
    }
    static List<LRWInfoDef> mLRWInfoList = new List<LRWInfoDef>();

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

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

        SplitAct(InputList[0]);
        long W = wkArr[0];

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            LRWInfoDef WillAdd;
            WillAdd.L = wkArr[0];
            WillAdd.R = wkArr[1];
            WillAdd.W = wkArr[2];
            mLRWInfoList.Add(WillAdd);
        }

        // 価値合計の最大値[重さ合計]なセグ木
        var InsSegmentTree = new SegmentTree(W + 1);
        InsSegmentTree.Update(0, 0);

        foreach (LRWInfoDef EachLRWInfo in mLRWInfoList) {
            for (long I = W; 0 <= I; I--) {
                long FromRangeSta = I - EachLRWInfo.R;
                long FromRangeEnd = I - EachLRWInfo.L;

                // 範囲外の場合
                if (FromRangeEnd < 0) break;

                FromRangeSta = Math.Max(FromRangeSta, 0);
                FromRangeEnd = Math.Max(FromRangeEnd, 0);

                long MaxVal = InsSegmentTree.Query(FromRangeSta, FromRangeEnd, 0);
                if (MaxVal < 0) continue;

                long CurrVal = InsSegmentTree.Query(I, I, 0);
                long NewVal = MaxVal + EachLRWInfo.W;
                if (CurrVal < NewVal) {
                    InsSegmentTree.Update(I, NewVal);
                }
            }
        }
        long AnswerVal = InsSegmentTree.Query(W, W, 0);
        if (AnswerVal < 0) {
            Console.WriteLine(-1);
        }
        else {
            Console.WriteLine(AnswerVal);
        }
    }
}

#region SegmentTree
// SegmentTreeクラス (RMaxQ and 1点更新)
internal class SegmentTree
{
    private long[] mTreeNodeArr;
    private long UB; // 木のノードの配列のUB
    private long mLeafCnt; // 葉ノードの数

    // ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列
    private struct RangeInfoDef
    {
        internal long StaInd;
        internal long EndInd;
    }
    private RangeInfoDef[] mRangeInfo;

    // コンストラクタ
    internal SegmentTree(long pLeafCnt)
    {
        // 簡単のため、葉ノード数を2のべき乗に
        long ArrLength = 0;
        for (long I = 1; I < long.MaxValue; I *= 2) {
            ArrLength += I;
            mLeafCnt = I;

            if (pLeafCnt < mLeafCnt) break;
        }

        // すべての値をlong.MinValueに
        UB = ArrLength - 1;
        mTreeNodeArr = new long[UB + 1];
        for (long I = 0; I <= UB; I++) {
            mTreeNodeArr[I] = long.MinValue;
        }

        // ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列の作成
        mRangeInfo = new RangeInfoDef[UB + 1];
        for (long I = 0; I <= UB; I++) {
            if (I == 0) {
                RangeInfoDef WillSet1;
                WillSet1.StaInd = 0;
                WillSet1.EndInd = mLeafCnt - 1;
                mRangeInfo[I] = WillSet1;
                continue;
            }
            long ParentNode = DeriveParentNode(I);
            RangeInfoDef ParentRangeInfo = mRangeInfo[ParentNode];

            RangeInfoDef WillSet2;
            long Mid = (ParentRangeInfo.StaInd + ParentRangeInfo.EndInd) / 2;

            if (I % 2 == 1) { // 奇数ノードの場合
                WillSet2.StaInd = ParentRangeInfo.StaInd;
                WillSet2.EndInd = Mid;
            }
            else { // 偶数ノードの場合
                WillSet2.StaInd = Mid + 1;
                WillSet2.EndInd = ParentRangeInfo.EndInd;
            }
            mRangeInfo[I] = WillSet2;
        }
    }

    // 親ノードの添字を取得
    private long DeriveParentNode(long pTarget)
    {
        return (pTarget - 1) / 2;
    }

    // 子ノードの添字(小さいほう)を取得
    private long DeriveChildNode(long pTarget)
    {
        return pTarget * 2 + 1;
    }

    // 葉ノードの配列の添字を木の添字に変換して返す
    private long DeriveTreeNode(long pLeafArrInd)
    {
        long BaseInd = UB - mLeafCnt + 1;
        return BaseInd + pLeafArrInd;
    }

    // 葉ノードの配列のK番目の値をNewValに変更
    internal void Update(long pK, long pNewVal)
    {
        long CurrNode = DeriveTreeNode(pK);
        mTreeNodeArr[CurrNode] = pNewVal;

        // 登りながら更新
        while (CurrNode > 0) {
            CurrNode = DeriveParentNode(CurrNode);
            long ChildNode1 = DeriveChildNode(CurrNode);
            long ChildNode2 = ChildNode1 + 1;
            mTreeNodeArr[CurrNode] =
                Math.Max(mTreeNodeArr[ChildNode1], mTreeNodeArr[ChildNode2]);
        }
    }

    // 開始添字と終了添字とカレントノードを引数として、最大値を返す
    internal long Query(long pSearchStaInd, long pSearchEndInd, long pCurrNode)
    {
        long CurrNodeStaInd = mRangeInfo[pCurrNode].StaInd;
        long CurrNodeEndInd = mRangeInfo[pCurrNode].EndInd;

        // OverLapしてなければ、long.MinValue
        if (CurrNodeEndInd < pSearchStaInd || pSearchEndInd < CurrNodeStaInd)
            return long.MinValue;

        // 完全に含んでいれば、このノードの値
        if (pSearchStaInd <= CurrNodeStaInd && CurrNodeEndInd <= pSearchEndInd)
            return mTreeNodeArr[pCurrNode];

        // そうでなければ、2つの子の最大値
        long ChildNode1 = DeriveChildNode(pCurrNode);
        long ChildNode2 = ChildNode1 + 1;

        long ChildVal1 = Query(pSearchStaInd, pSearchEndInd, ChildNode1);
        long ChildVal2 = Query(pSearchStaInd, pSearchEndInd, ChildNode2);
        return Math.Max(ChildVal1, ChildVal2);
    }
}
#endregion


解説

貰うDPを、セグ木を使って高速化してます。