AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC141-D Powerful Discount Tickets
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 3");
WillReturn.Add("2 13 8");
//9
}
else if (InputPattern == "Input2") {
WillReturn.Add("4 4");
WillReturn.Add("1 9 3 5");
//6
}
else if (InputPattern == "Input3") {
WillReturn.Add("1 100000");
WillReturn.Add("1000000000");
//0
}
else if (InputPattern == "Input4") {
WillReturn.Add("10 1");
WillReturn.Add("1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000");
//9500000000
}
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 M = wkArr[1];
int RestM = M;
int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
PQueue.PQueueJyoutaiDef WillEnqueue;
var InsPQueue = new PQueue();
foreach (int EachInt in AArr) {
WillEnqueue.Cost = EachInt;
InsPQueue.Enqueue(WillEnqueue);
}
long Answer = 0;
while (InsPQueue.IsEmpty() == false) {
PQueue.PQueueJyoutaiDef Dequeued = InsPQueue.Dequeue();
if (Dequeued.Cost == 0) continue;
if (RestM > 0) {
WillEnqueue.Cost = Dequeued.Cost / 2;
InsPQueue.Enqueue(WillEnqueue);
RestM--;
continue;
}
Answer += Dequeued.Cost;
}
Console.WriteLine(Answer);
}
}
#region PQueue
// 優先度付きキュー (根のCostが最大)
internal class PQueue
{
internal struct PQueueJyoutaiDef
{
internal int Cost;
}
private Dictionary<int, PQueueJyoutaiDef> mHeapDict = new Dictionary<int, PQueueJyoutaiDef>();
internal bool IsEmpty()
{
return mHeapDict.Count == 0;
}
// エンキュー処理
internal void Enqueue(PQueueJyoutaiDef pAddJyoutai)
{
int CurrNode = 1 + mHeapDict.Count;
mHeapDict[CurrNode] = pAddJyoutai;
while (1 < CurrNode && mHeapDict[CurrNode / 2].Cost < mHeapDict[CurrNode].Cost) {
PQueueJyoutaiDef Swap = mHeapDict[CurrNode];
mHeapDict[CurrNode] = mHeapDict[CurrNode / 2];
mHeapDict[CurrNode / 2] = Swap;
CurrNode /= 2;
}
}
// デキュー処理
internal PQueueJyoutaiDef Dequeue()
{
PQueueJyoutaiDef TopNode = mHeapDict[1];
int LastNode = mHeapDict.Count;
mHeapDict[1] = mHeapDict[LastNode];
mHeapDict.Remove(LastNode);
MinHeapify(1);
return TopNode;
}
// 根ノードを指定し、根から葉へヒープ構築
private void MinHeapify(int pRootNode)
{
if (mHeapDict.Count <= 1) {
return;
}
int Left = pRootNode * 2;
int Right = pRootNode * 2 + 1;
// 左の子、自分、右の子で値が最大のノードを選ぶ
int Smallest = mHeapDict[pRootNode].Cost;
int SmallestNode = pRootNode;
if (mHeapDict.ContainsKey(Left) && mHeapDict[Left].Cost > Smallest) {
Smallest = mHeapDict[Left].Cost;
SmallestNode = Left;
}
if (mHeapDict.ContainsKey(Right) && mHeapDict[Right].Cost > Smallest) {
Smallest = mHeapDict[Right].Cost;
SmallestNode = Right;
}
// 子ノードのほうが大きい場合
if (SmallestNode != pRootNode) {
PQueueJyoutaiDef Swap = mHeapDict[SmallestNode];
mHeapDict[SmallestNode] = mHeapDict[pRootNode];
mHeapDict[pRootNode] = Swap;
// 再帰的に呼び出し
MinHeapify(SmallestNode);
}
}
}
#endregion
解説
プライオリティキューを使って
最大値を半分にする処理を繰り返してます。