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

ABC130-D Enough Array


問題へのリンク


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("4 10");
            WillReturn.Add("6 1 2 7");
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 5");
            WillReturn.Add("3 3 3");
            //3
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10 53462");
            WillReturn.Add("103 35322 232 342 21099 90000 18843 9010 35221 19352");
            //36
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long K = wkArr[1];

        long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();

        long UB = AArr.GetUpperBound(0);

        // 尺取法で解く
        long R = 0;
        long CurrSum = AArr[0];
        long Answer = 0;

        for (long L = 0; L <= UB; L++) {
            while (R + 1 <= UB && CurrSum < K) {
                R++;
                CurrSum += AArr[R];
            }

            if (CurrSum >= K) {
                Answer += UB - R + 1;
            }
            CurrSum -= AArr[L];
        }
        Console.WriteLine(Answer);
    }
}


解説

尺取法で解いてます。