AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC208-C Fair Candy Distribution
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("2 7");
WillReturn.Add("1 8");
//4
//3
}
else if (InputPattern == "Input2") {
WillReturn.Add("1 3");
WillReturn.Add("33");
//3
}
else if (InputPattern == "Input3") {
WillReturn.Add("7 1000000000000");
WillReturn.Add("99 8 2 4 43 5 3");
//142857142857
//142857142857
//142857142858
//142857142857
//142857142857
//142857142857
//142857142857
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long N = wkArr[0];
long K = wkArr[1];
long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
// 全員に配布できる分
long SameCnt = K / N;
// 残りの端数
long RestCnt = K % N;
var HaihuCntDict = new Dictionary<long, long>();
long[] SortedAArr = AArr.OrderBy(pX => pX).ToArray();
for (int I = 0; I <= SortedAArr.GetUpperBound(0); I++) {
long HaihuCnt = SameCnt;
if (RestCnt > 0) {
HaihuCnt++;
RestCnt--;
}
HaihuCntDict[SortedAArr[I]] = HaihuCnt;
}
foreach (long EachA in AArr) {
Console.WriteLine(HaihuCntDict[EachA]);
}
}
}
解説
割り算して余りがあったら、昇順に分配してます。