AOJ本の読書メモ
AOJ
次のAOJの問題へ
前のAOJの問題へ
AOJ 0579 暑い日々 (Hot days)
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 4");
WillReturn.Add("31");
WillReturn.Add("27");
WillReturn.Add("35");
WillReturn.Add("20 25 30");
WillReturn.Add("23 29 90");
WillReturn.Add("21 35 60");
WillReturn.Add("28 33 40");
}
else if (InputPattern == "Input2") {
WillReturn.Add("5 2");
WillReturn.Add("26");
WillReturn.Add("28");
WillReturn.Add("32");
WillReturn.Add("29");
WillReturn.Add("34");
WillReturn.Add("30 35 0");
WillReturn.Add("25 30 100");
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct FukuInfoDef
{
internal int MinOndo;
internal int MaxOndo;
internal int Hade;
}
static List<FukuInfoDef> mFukuInfoList = new List<FukuInfoDef>();
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]);
int D = wkArr[0];
int[] DArr = InputList.Skip(1).Take(D).Select(pX => int.Parse(pX)).ToArray();
foreach (string EachStr in InputList.Skip(1 + D)) {
SplitAct(EachStr);
FukuInfoDef WillAdd;
WillAdd.MinOndo = wkArr[0];
WillAdd.MaxOndo = wkArr[1];
WillAdd.Hade = wkArr[2];
mFukuInfoList.Add(WillAdd);
}
int UB = mFukuInfoList.Max(pX => pX.Hade);
// 最大スコア[最後の服の派手さ]なDP表
int?[] PrevDP = new int?[UB + 1];
int FirstD = DArr[0];
foreach (FukuInfoDef EachFukuInfo in mFukuInfoList) {
if (EachFukuInfo.MinOndo <= FirstD && FirstD <= EachFukuInfo.MaxOndo) {
PrevDP[EachFukuInfo.Hade] = 0;
}
}
foreach (int EachOndo in DArr.Skip(1)) {
int?[] CurrDP = new int?[UB + 1];
var CurrFukuInfoList = mFukuInfoList.Where(pX =>
pX.MinOndo <= EachOndo && EachOndo <= pX.MaxOndo).ToList();
for (int I = 0; I <= UB; I++) {
if (PrevDP[I].HasValue == false) continue;
foreach (FukuInfoDef EachFukuInfo in CurrFukuInfoList) {
int NewI = EachFukuInfo.Hade;
int AddScore = Math.Abs(I - EachFukuInfo.Hade);
int NewVal = PrevDP[I].Value + AddScore;
if (CurrDP[NewI].HasValue) {
if (CurrDP[NewI] >= NewVal) {
continue;
}
}
CurrDP[NewI] = NewVal;
}
}
PrevDP = CurrDP;
}
Console.WriteLine(PrevDP.Max());
}
}
解説
最大スコア[最後の服の派手さ]を更新するDPで解いてます。