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

ABC188-D Snuke Prime


問題へのリンク


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 6");
            WillReturn.Add("1 2 4");
            WillReturn.Add("2 2 4");
            //10
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 1000000000");
            WillReturn.Add("583563238 820642330 44577");
            WillReturn.Add("136809000 653199778 90962");
            WillReturn.Add("54601291 785892285 50554");
            WillReturn.Add("5797762 453599267 65697");
            WillReturn.Add("468677897 916692569 87409");
            //163089627821228
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("5 100000");
            WillReturn.Add("583563238 820642330 44577");
            WillReturn.Add("136809000 653199778 90962");
            WillReturn.Add("54601291 785892285 50554");
            WillReturn.Add("5797762 453599267 65697");
            WillReturn.Add("468677897 916692569 87409");
            //88206004785464
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct ABCInfoDef
    {
        internal long A;
        internal long B;
        internal long C;
    }

    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 C = wkArr[1];

        var ABCInfoList = new List<ABCInfoDef>();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            ABCInfoDef WillAdd;
            WillAdd.A = wkArr[0];
            WillAdd.B = wkArr[1];
            WillAdd.C = wkArr[2];
            ABCInfoList.Add(WillAdd);
        }

        var RunSumDict = new Dictionary<long, long>();

        Action<long, long> AddAct = (pKey, pVal) =>
        {
            if (RunSumDict.ContainsKey(pKey) == false) {
                RunSumDict[pKey] = pVal;
            }
            else {
                RunSumDict[pKey] += pVal;
            }
        };
        foreach (ABCInfoDef EachABCInfo in ABCInfoList) {
            AddAct(EachABCInfo.A, EachABCInfo.C);
            AddAct(EachABCInfo.B + 1, -EachABCInfo.C);
        }

        // 座標圧縮する
        long[] ZaatuArr = RunSumDict.Keys.ToArray();
        Array.Sort(ZaatuArr);
        long UB = ZaatuArr.GetUpperBound(0);

        // 累積和を求める
        for (int I = 1; I <= UB; I++) {
            long PrevKey = ZaatuArr[I - 1];
            long CurrKey = ZaatuArr[I];

            RunSumDict[CurrKey] += RunSumDict[PrevKey];
        }

        long Answer = 0;
        for (int I = 0; I <= UB - 1; I++) {
            long CurrKey = ZaatuArr[I];
            long NextKey = ZaatuArr[I + 1];

            long Curr_Cost = RunSumDict[CurrKey];
            if (Curr_Cost == 0) continue;
            long Day1_Cost = Math.Min(C, Curr_Cost);
            Answer += (NextKey - CurrKey) * Day1_Cost;
        }
        Console.WriteLine(Answer);
    }
}


解説

いもす法と座標圧縮を組み合わせてます。


類題

ABC221-D Online games