yukicoder
前のyukicoderの問題へ
yukicoder 3302 Sense Battle
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");
WillReturn.Add("200 100");
WillReturn.Add("300 500");
WillReturn.Add("500 200");
WillReturn.Add("700 100");
//1500
}
else if (InputPattern == "Input2") {
WillReturn.Add("1");
WillReturn.Add("1000000000 1");
//1
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static long[] GetSplitArr(string pStr)
{
pStr = pStr.TrimEnd();
return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
}
struct ItemInfoDef
{
internal long AddPower;
internal long AttackPlus;
}
static List<ItemInfoDef> mItemInfoList = new List<ItemInfoDef>();
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = { };
Action<string> SplitAct = (pStr) => wkArr = GetSplitArr(pStr);
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
ItemInfoDef WillAdd;
WillAdd.AddPower = wkArr[0];
WillAdd.AttackPlus = wkArr[1];
mItemInfoList.Add(WillAdd);
}
mItemInfoList.Reverse();
long UB = mItemInfoList.Count;
// 最大スコア[攻撃回数]なインラインDP表
long?[] DPArr = new long?[UB + 1];
DPArr[0] = 0;
long Answer = long.MinValue;
foreach (ItemInfoDef EachItemInfo in mItemInfoList) {
for (long I = UB; 0 <= I; I--) {
if (DPArr[I].HasValue == false) continue;
Action<long, long> UpdateAct = (pNewInd, pNewVal) =>
{
if (DPArr[pNewInd].HasValue) {
if (DPArr[pNewInd] >= pNewVal) {
return;
}
}
DPArr[pNewInd] = pNewVal;
Answer = Math.Max(Answer, pNewVal);
};
// 攻撃する場合
UpdateAct(I + 1, DPArr[I].Value + EachItemInfo.AttackPlus);
// 力を増やす場合
UpdateAct(I, DPArr[I].Value + EachItemInfo.AddPower * I);
}
}
Console.WriteLine(Answer);
}
}
解説
掛け算の分配法則をふまえ、
最大スコア[攻撃回数]で逆からインラインDPしてます。
攻撃回数は、変わらないか、増えるかなので
インラインDPにできます。