AOJ本の読書メモ
AOJ
次のAOJの問題へ
前のAOJの問題へ
DSL_3_A: The Smallest Window I
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("6 5");
WillReturn.Add("1 2 3 4 5 6");
WillReturn.Add("6 9 12 21 15");
//9
//12
//15
//21
//18
}
else if (InputPattern == "Input2") {
WillReturn.Add("6 5");
WillReturn.Add("5 10 8 8 8 1");
WillReturn.Add("27 1 30 18 3");
//16
//1
//16
//12
//1
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long[] QArr = InputList[2].Split(' ').Select(pX => long.Parse(pX)).ToArray();
foreach (long EachQ in QArr) {
long Answer = 0;
long CurrSum = AArr[0];
long R = 0;
long UB = AArr.GetUpperBound(0);
for (long L = 0; L <= UB; L++) {
if (R < L) {
R = L;
CurrSum = AArr[L];
}
while (R + 1 <= UB && CurrSum + AArr[R + 1] <= EachQ) {
CurrSum += AArr[R + 1];
R++;
}
if (L == R) {
if (CurrSum <= EachQ) {
Answer++;
}
}
else {
Answer += R - L + 1;
}
CurrSum -= AArr[L];
}
Console.WriteLine(Answer);
}
}
}
解説
二分探索だとTLEしたので、
尺取法を使ってます。