AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC153-E Crested Ibis vs Monster
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("9 3");
WillReturn.Add("8 3");
WillReturn.Add("4 2");
WillReturn.Add("2 1");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("100 6");
WillReturn.Add("1 1");
WillReturn.Add("2 3");
WillReturn.Add("3 9");
WillReturn.Add("4 27");
WillReturn.Add("5 81");
WillReturn.Add("6 243");
//100
}
else if (InputPattern == "Input3") {
WillReturn.Add("9999 10");
WillReturn.Add("540 7550");
WillReturn.Add("691 9680");
WillReturn.Add("700 9790");
WillReturn.Add("510 7150");
WillReturn.Add("415 5818");
WillReturn.Add("551 7712");
WillReturn.Add("587 8227");
WillReturn.Add("619 8671");
WillReturn.Add("588 8228");
WillReturn.Add("176 2461");
//139815
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct ABInfoDef
{
internal long A;
internal long B;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(InputList[0]);
long H = wkArr[0];
long UB = H;
var ABInfoList = new List<ABInfoDef>();
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
ABInfoDef WillAdd;
WillAdd.A = wkArr[0];
WillAdd.B = wkArr[1];
ABInfoList.Add(WillAdd);
}
// 最小の消費魔力[モンスターのHP]なDP表
long?[] DPArr = new long?[UB + 1];
DPArr[UB] = 0;
foreach (ABInfoDef EachABInfo in ABInfoList) {
for (long I = UB; 1 <= I; I--) {
if (DPArr[I].HasValue == false) continue;
long NewInd = I - EachABInfo.A;
NewInd = Math.Max(0, NewInd);
long NewVal = DPArr[I].Value + EachABInfo.B;
if (DPArr[NewInd].HasValue) {
if (DPArr[NewInd].Value <= NewVal) {
continue;
}
}
DPArr[NewInd] = NewVal;
}
}
Console.WriteLine(DPArr[0]);
}
}
解説
DPで解いてます。