競技プログラミングの鉄則
次の問題へ
前の問題へ
B39 Taro's Job
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 4");
WillReturn.Add("1 1");
WillReturn.Add("2 4");
WillReturn.Add("2 3");
WillReturn.Add("3 4");
WillReturn.Add("4 2");
//12
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(InputList[0]);
int D = wkArr[1];
// YList[Xの値]なDict
var YListDict = new Dictionary<int, List<int>>();
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
int X = wkArr[0];
int Y = wkArr[1];
if (YListDict.ContainsKey(X) == false) {
YListDict[X] = new List<int>();
}
YListDict[X].Add(Y);
}
var InsPQueue = new PQueue();
long Answer = 0;
for (int I = 1; I <= D; I++) {
if (YListDict.ContainsKey(I)) {
foreach (int EachY in YListDict[I]) {
PQueue.PQueueJyoutaiDef WillEnqueue;
WillEnqueue.Val = EachY;
InsPQueue.Enqueue(WillEnqueue);
}
}
if (InsPQueue.IsEmpty() == false) {
PQueue.PQueueJyoutaiDef Dequeued = InsPQueue.Dequeue();
Answer += Dequeued.Val;
}
}
Console.WriteLine(Answer);
}
}
#region PQueue
// 優先度付きキュー (根のValが最大)
internal class PQueue
{
internal struct PQueueJyoutaiDef
{
internal long Val;
}
private Dictionary<long, PQueueJyoutaiDef> mHeapDict = new Dictionary<long, PQueueJyoutaiDef>();
internal bool IsEmpty()
{
return mHeapDict.Count == 0;
}
internal long Count()
{
return mHeapDict.Count;
}
internal long Peek()
{
return mHeapDict[1].Val;
}
// エンキュー処理
internal void Enqueue(PQueueJyoutaiDef pAddJyoutai)
{
long CurrNode = 1 + mHeapDict.Count;
mHeapDict[CurrNode] = pAddJyoutai;
while (1 < CurrNode && mHeapDict[CurrNode / 2].Val < mHeapDict[CurrNode].Val) {
PQueueJyoutaiDef Swap = mHeapDict[CurrNode];
mHeapDict[CurrNode] = mHeapDict[CurrNode / 2];
mHeapDict[CurrNode / 2] = Swap;
CurrNode /= 2;
}
}
// デキュー処理
internal PQueueJyoutaiDef Dequeue()
{
PQueueJyoutaiDef TopNode = mHeapDict[1];
long LastNode = mHeapDict.Count;
mHeapDict[1] = mHeapDict[LastNode];
mHeapDict.Remove(LastNode);
MinHeapify(1);
return TopNode;
}
// 根ノードを指定し、根から葉へヒープ構築
private void MinHeapify(long pRootNode)
{
if (mHeapDict.Count <= 1) {
return;
}
long Left = pRootNode * 2;
long Right = pRootNode * 2 + 1;
// 左の子、自分、右の子で値が最大のノードを選ぶ
long Smallest = mHeapDict[pRootNode].Val;
long SmallestNode = pRootNode;
if (mHeapDict.ContainsKey(Left) && mHeapDict[Left].Val > Smallest) {
Smallest = mHeapDict[Left].Val;
SmallestNode = Left;
}
if (mHeapDict.ContainsKey(Right) && mHeapDict[Right].Val > Smallest) {
Smallest = mHeapDict[Right].Val;
SmallestNode = Right;
}
// 子ノードのほうが大きい場合
if (SmallestNode != pRootNode) {
PQueueJyoutaiDef Swap = mHeapDict[SmallestNode];
mHeapDict[SmallestNode] = mHeapDict[pRootNode];
mHeapDict[pRootNode] = Swap;
// 再帰的に呼び出し
MinHeapify(SmallestNode);
}
}
}
#endregion
解説
可能になった仕事で
スコアが最大の仕事を選択する貪欲法で解いてます。