square869120Contest
次のsquare869120Contestの問題へ
前のsquare869120Contestの問題へ
square869120コンテスト5 A問題 Sushi 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("1 6");
WillReturn.Add("4");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 10");
WillReturn.Add("3 7 2");
//12
}
else if (InputPattern == "Input3") {
WillReturn.Add("6 15");
WillReturn.Add("8 6 9 1 2 0");
//45
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int T = wkArr[1];
int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int CurrTime = 0;
foreach (int EachA in AArr) {
int Katamuki = T;
int Seppen = EachA;
int Uhen = CurrTime;
int X = Solve(Katamuki, Seppen, Uhen);
CurrTime = Katamuki * X + Seppen;
}
Console.WriteLine(CurrTime);
}
// 不等式 SX + T >= U
// をXについて解く
static int Solve(int pKatamuki, int pSeppen, int pUhen)
{
pUhen -= pSeppen;
int Answer = pUhen / pKatamuki;
if (pUhen % pKatamuki > 0) Answer++;
return Answer;
}
}
解説
貪欲法で解いてます。