AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC128-D equeue
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("6 4");
WillReturn.Add("-10 8 2 1 2 6");
//14
}
else if (InputPattern == "Input2") {
WillReturn.Add("6 4");
WillReturn.Add("-6 -100 50 -2 -5 -3");
//44
}
else if (InputPattern == "Input3") {
WillReturn.Add("6 3");
WillReturn.Add("-6 -100 50 -2 -5 -3");
//0
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int[] mVArr;
static int UB;
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int K = wkArr[1];
mVArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
UB = mVArr.GetUpperBound(0);
int Answer = int.MinValue;
for (int A = 0; A <= K; A++) {
for (int B = 0; A + B <= K; B++) {
for (int C = 0; A + B + C <= K; C++) {
Answer = Math.Max(Answer, DeriveScore(A, B, C));
}
}
}
Console.WriteLine(Answer);
}
// 左からA個取って、右からB個取って、C個捨てた時の得点を返す
static int DeriveScore(int pA, int pB, int pC)
{
var TakeList = new List<int>();
if (pA + pB < mVArr.Length) {
TakeList.AddRange(mVArr.Take(pA));
for (int I = UB; UB - pB + 1 <= I; I--) {
TakeList.Add(mVArr[I]);
}
}
else {
TakeList = mVArr.ToList();
}
TakeList.Sort();
for (int I = 1; I <= pC; I++) {
if (TakeList.Count > 0 && TakeList[0] < 0) {
TakeList.RemoveAt(0);
}
else break;
}
return TakeList.Sum();
}
}
解説
捨てる処理は最後にまとめてやっても問題ないので
左から取る個数。右から取る個数。捨てる個数
を全探索してます。