典型90問
次の典型90問へ
前の典型90問へ
典型90問 048 I will not drop out(★3)
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 3");
WillReturn.Add("4 3");
WillReturn.Add("9 5");
WillReturn.Add("15 8");
WillReturn.Add("8 6");
//21
}
else if (InputPattern == "Input2") {
WillReturn.Add("2 2");
WillReturn.Add("7 6");
WillReturn.Add("3 2");
//8
}
else if (InputPattern == "Input3") {
WillReturn.Add("10 12");
WillReturn.Add("987753612 748826789");
WillReturn.Add("36950727 36005047");
WillReturn.Add("961239509 808587458");
WillReturn.Add("905633062 623962559");
WillReturn.Add("940964276 685396947");
WillReturn.Add("959540552 928301562");
WillReturn.Add("60467784 37828572");
WillReturn.Add("953685176 482123245");
WillReturn.Add("87983282 66762644");
WillReturn.Add("912605260 709048491");
//6437530406
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct ABInfoDef
{
internal long A;
internal long B;
}
static List<ABInfoDef> mABInfoList = new List<ABInfoDef>();
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();
SplitAct(InputList[0]);
long K = wkArr[1];
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
ABInfoDef WillAdd;
WillAdd.A = wkArr[0];
WillAdd.B = wkArr[1];
mABInfoList.Add(WillAdd);
}
var ScoreList = new List<long>();
foreach (ABInfoDef EachABInfo in mABInfoList) {
ScoreList.Add(EachABInfo.B);
ScoreList.Add(EachABInfo.A - EachABInfo.B);
}
var Query = ScoreList.OrderByDescending(pX => pX).Take((int)K);
Console.WriteLine(Query.Sum());
}
}
解説
考察すれば貪欲法が使えると分かります。