AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC257-E Addition and Multiplication 2
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("5");
WillReturn.Add("5 4 3 3 2 5 3 5 3");
//95
}
else if (InputPattern == "Input2") {
WillReturn.Add("20");
WillReturn.Add("1 1 1 1 1 1 1 1 1");
//99999999999999999999
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int N = int.Parse(InputList[0]);
int[] CArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int MinCost = CArr.Min();
char BaseChar = '?';
for (int I = CArr.GetUpperBound(0); 0 <= I; I--) {
if (CArr[I] == MinCost) {
BaseChar = (I + 1).ToString()[0];
break;
}
}
string AnswerStr = new string(BaseChar, N / MinCost);
int Rest = N % MinCost;
char[] AnswerArr = AnswerStr.ToCharArray();
for (int I = 0; I <= AnswerArr.GetUpperBound(0); I++) {
for (int J = CArr.GetUpperBound(0); 0 <= J; J--) {
char CurrChar = (J + 1).ToString()[0];
if (CurrChar <= BaseChar) break;
int NeedCost = CArr[J] - MinCost;
if (NeedCost <= Rest) {
AnswerArr[I] = CurrChar;
Rest -= NeedCost;
break;
}
}
if (Rest <= 0) break;
}
Console.WriteLine(new string(AnswerArr.ToArray()));
}
}
解説
桁数を最優先で増やすのが最適なので
まずは、コストが最小の中で最大の数字を
可能な限り並べます。
次に、余った金額で
上位桁から順に、
9,8,7,6,5,4,3,2の優先順位で変更可能かを見てます。