Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace CubicArtilery
- {
- using System;
- using System.Collections.Generic;
- class Program
- {
- static void Main(string[] args)
- {
- var maxCapacity = int.Parse(Console.ReadLine());
- var bunkersOrder = new Queue<string>();
- var bunkers = new Dictionary<string, Queue<int>>();
- var bunkersFullness = new Dictionary<string, int>();
- var input = Console.ReadLine();
- while (input != "Bunker Revision")
- {
- var bunkersInfo = input.Split();
- int weaponCapacity;
- foreach (var element in bunkersInfo)
- {
- if (!int.TryParse(element,out weaponCapacity))
- {
- bunkers.Add(element, new Queue<int>());
- bunkersFullness.Add(element, 0);
- bunkersOrder.Enqueue(element);
- }
- else
- {
- var currentBunker = bunkersOrder.Peek();
- if (maxCapacity - bunkersFullness[currentBunker] >= weaponCapacity)
- {
- bunkers[currentBunker].Enqueue(weaponCapacity);
- bunkersFullness[currentBunker] += weaponCapacity;
- }
- else
- {
- if (bunkersOrder.Count > 1)
- {
- PrintBunker(new KeyValuePair<string, Queue<int>>(currentBunker, bunkers[currentBunker]));
- bunkersOrder.Dequeue();
- bunkers.Remove(currentBunker);
- bunkersFullness.Remove(currentBunker);
- foreach (var b in bunkersOrder)
- {
- if (maxCapacity - bunkersFullness[b] >= weaponCapacity)
- {
- bunkers[b].Enqueue(weaponCapacity);
- bunkersFullness[b] += weaponCapacity;
- }
- }
- }
- else
- {
- if (weaponCapacity <= maxCapacity)
- {
- TryFitInBunker(weaponCapacity, currentBunker, bunkers, bunkersFullness, maxCapacity);
- }
- }
- }
- }
- }
- input = Console.ReadLine();
- }
- }
- private static void TryFitInBunker(int weaponCapacity, string currentBunker, Dictionary<string, Queue<int>> bunkers, Dictionary<string,int> bunkersFullness, int maxCapacity)
- {
- while (bunkersFullness[currentBunker] + weaponCapacity > maxCapacity)
- {
- bunkersFullness[currentBunker] -= bunkers[currentBunker].Dequeue();
- }
- bunkers[currentBunker].Enqueue(weaponCapacity);
- bunkersFullness[currentBunker] += weaponCapacity;
- }
- private static void PrintBunker(KeyValuePair<string, Queue<int>> bunker)
- {
- if (bunker.Value.Count == 0)
- {
- Console.WriteLine($"{bunker.Key} -> Empty");
- }
- else
- {
- Console.WriteLine($"{bunker.Key} -> {string.Join(", ",bunker.Value)}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement