AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC467-C Adjacent Sums (easy)
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 2");
WillReturn.Add("1 1 1");
WillReturn.Add("1 1");
//1
}
else if (InputPattern == "Input2") {
WillReturn.Add("2 2");
WillReturn.Add("1 1");
WillReturn.Add("0");
//0
}
else if (InputPattern == "Input3") {
WillReturn.Add("10 2");
WillReturn.Add("0 0 0 1 1 0 1 0 1 0");
WillReturn.Add("0 1 0 1 0 1 0 1 0");
//4
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static long[] GetSplitArr(string pStr)
{
return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
}
static long[] mAArr;
static long[] mBArr;
static void Main()
{
List<string> InputList = GetInputList();
mAArr = GetSplitArr(InputList[1]);
mBArr = GetSplitArr(InputList[2]);
var CostList = new List<long>();
long[] CurrAArr1 = (long[])mAArr.Clone();
long Cost1 = DeriveCost(CurrAArr1);
long[] CurrAArr2 = (long[])mAArr.Clone();
CurrAArr2[0]++;
long Cost2 = 1 + DeriveCost(CurrAArr2);
Console.WriteLine(Cost1 < Cost2 ? Cost1 : Cost2);
}
// コストを返す
static long DeriveCost(long[] pAArr)
{
long Cost = 0;
for (long I = 1; I <= pAArr.GetUpperBound(0); I++) {
long Sum = pAArr[I - 1] + pAArr[I];
Sum %= 2;
if (Sum != mBArr[I - 1]) {
pAArr[I]++;
Cost++;
}
}
return Cost;
}
}
解説
数列Aの初項を決めると
残りの項が決定するので
初項を0と1の2通りで、
総コストの低いほうが解になります。