AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC323-D Merge Slimes
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("3");
WillReturn.Add("3 3");
WillReturn.Add("5 1");
WillReturn.Add("6 1");
//3
}
else if (InputPattern == "Input2") {
WillReturn.Add("3");
WillReturn.Add("1 1");
WillReturn.Add("2 1");
WillReturn.Add("3 1");
//3
}
else if (InputPattern == "Input3") {
WillReturn.Add("1");
WillReturn.Add("1000000000 1000000000");
//13
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();
// 個数[スライムのサイズ]なDict
var CntDict = new Dictionary<long, long>();
// 見る必要があるサイズ
var InsPQueueArr = new PQueue_Arr();
PQueue_Arr.PQueueJyoutaiDef WillEnqueue;
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
long Size = wkArr[0];
long Cnt = wkArr[1];
if (CntDict.ContainsKey(Size) == false) {
CntDict[Size] = 0;
}
CntDict[Size] += Cnt;
WillEnqueue.Val = Size;
InsPQueueArr.Enqueue(WillEnqueue);
}
long Answer = 0;
while (InsPQueueArr.Count() > 0) {
PQueue_Arr.PQueueJyoutaiDef Dequeued = InsPQueueArr.Dequeue();
long CurrCnt = CntDict[Dequeued.Val];
if (CurrCnt % 2 == 1) {
Answer++;
CurrCnt--;
}
long NextSize = Dequeued.Val * 2;
if (CntDict.ContainsKey(NextSize) == false) {
CntDict[NextSize] = 0;
}
if (CurrCnt / 2 > 0) {
CntDict[NextSize] += CurrCnt / 2;
WillEnqueue.Val = NextSize;
InsPQueueArr.Enqueue(WillEnqueue);
}
CntDict[Dequeued.Val] = 0;
}
Console.WriteLine(Answer);
}
}
#region PQueue_Arr
// 内部で配列使用の優先度付きキュー (根のValが最小)
internal class PQueue_Arr
{
internal struct PQueueJyoutaiDef
{
internal long Val;
}
private PQueueJyoutaiDef[] mHeapArr;
private long mHeapArrCnt = 0;
// コンストラクタ
internal PQueue_Arr()
{
mHeapArr = new PQueueJyoutaiDef[65535];
}
internal bool IsEmpty()
{
return mHeapArrCnt == 0;
}
internal long Count()
{
return mHeapArrCnt;
}
internal long Peek()
{
return mHeapArr[1].Val;
}
// エンキュー処理
internal void Enqueue(PQueueJyoutaiDef pAddJyoutai)
{
long CurrNode = 1 + mHeapArrCnt;
if (mHeapArr.GetUpperBound(0) < CurrNode) {
ExtendArr();
}
mHeapArr[CurrNode] = pAddJyoutai;
mHeapArrCnt++;
while (1 < CurrNode && mHeapArr[CurrNode / 2].Val > mHeapArr[CurrNode].Val) {
PQueueJyoutaiDef Swap = mHeapArr[CurrNode];
mHeapArr[CurrNode] = mHeapArr[CurrNode / 2];
mHeapArr[CurrNode / 2] = Swap;
CurrNode /= 2;
}
}
// 配列のExtend
private void ExtendArr()
{
PQueueJyoutaiDef[] NewHeapArr = new PQueueJyoutaiDef[mHeapArrCnt * 2];
mHeapArr.CopyTo(NewHeapArr, 0);
mHeapArr = NewHeapArr;
}
// デキュー処理
internal PQueueJyoutaiDef Dequeue()
{
PQueueJyoutaiDef TopNode = mHeapArr[1];
long LastNode = mHeapArrCnt;
mHeapArr[1] = mHeapArr[LastNode];
mHeapArrCnt--;
MinHeapify(1);
return TopNode;
}
// 根ノードを指定し、根から葉へヒープ構築
private void MinHeapify(long pRootNode)
{
if (mHeapArrCnt <= 1) {
return;
}
long Left = pRootNode * 2;
long Right = pRootNode * 2 + 1;
// 左の子、自分、右の子で値が最小のノードを選ぶ
long Smallest = mHeapArr[pRootNode].Val;
long SmallestNode = pRootNode;
if (Left <= mHeapArrCnt && mHeapArr[Left].Val < Smallest) {
Smallest = mHeapArr[Left].Val;
SmallestNode = Left;
}
if (Right <= mHeapArrCnt && mHeapArr[Right].Val < Smallest) {
Smallest = mHeapArr[Right].Val;
SmallestNode = Right;
}
// 子ノードのほうが小さい場合
if (SmallestNode != pRootNode) {
PQueueJyoutaiDef Swap = mHeapArr[SmallestNode];
mHeapArr[SmallestNode] = mHeapArr[pRootNode];
mHeapArr[pRootNode] = Swap;
// 再帰的に呼び出し
MinHeapify(SmallestNode);
}
}
}
#endregion
解説
スライムのサイズの昇順に合成してます。