Created
October 11, 2019 09:50
-
-
Save ApprenticeGC/1c001d4a9ee04c52f7d4bf47807f05ae to your computer and use it in GitHub Desktop.
For coding game Magic Stone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ConsoleApplication1 | |
{ | |
using System; | |
using System.Linq; | |
using System.IO; | |
using System.Text; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class Solution | |
{ | |
public static void Main(string[] args) | |
{ | |
var cacheDict = new Dictionary<int, int>(); | |
int N = int.Parse(Console.ReadLine()); | |
string[] inputs = Console.ReadLine().Split(' '); | |
for (int i = 0; i < N; i++) | |
{ | |
int level = int.Parse(inputs[i]); | |
int count = 0; | |
bool hasCount = cacheDict.TryGetValue(level, out count); | |
if (!hasCount) | |
{ | |
cacheDict.Add(level, count); | |
} | |
cacheDict[level] = cacheDict[level] + 1; | |
} | |
var sortedPairs = cacheDict.OrderBy(pair => pair.Key).ToList(); | |
// var desc = sortedPairs.Aggregate("", (acc, next) => | |
// { | |
// var key = next.Key; | |
// var v = next.Value; | |
// var combined = acc + "\n" + key.ToString() + ": " + v.ToString(); | |
// return combined; | |
// }); | |
// Console.WriteLine(desc); | |
var currentIndex = 0; | |
var keepGoing = true; | |
// var outList = new List<KeyValuePair<int, int>>(); | |
while (keepGoing) | |
{ | |
var thePair = sortedPairs[currentIndex]; | |
var currentTier = thePair.Key; | |
var nextTier = thePair.Key + 1; | |
var combinedCount = thePair.Value / 2; | |
var remainingCount = thePair.Value % 2; | |
sortedPairs[currentIndex] = new KeyValuePair<int, int>(currentTier, remainingCount); | |
var hasNextTier = sortedPairs.Exists(sp => sp.Key == nextTier); | |
if (hasNextTier) | |
{ | |
// Console.WriteLine("hasNextTier"); | |
var nextTierIndex = sortedPairs.FindIndex(sp => sp.Key == nextTier); | |
sortedPairs[nextTierIndex] = new KeyValuePair<int, int>(nextTier, sortedPairs[nextTierIndex].Value + combinedCount); | |
} | |
else | |
{ | |
// var des = nextTier.ToString() + ": " + combinedCount.ToString(); | |
// Console.WriteLine("not hasNextTier " + des); | |
sortedPairs.Add(new KeyValuePair<int, int>(nextTier, combinedCount)); | |
sortedPairs = sortedPairs.OrderBy(sp => sp.Key).ToList(); | |
} | |
// Console.WriteLine("not hasNextTier " + sortedPairs.Count.ToString()); | |
++currentIndex; | |
if (currentIndex >= sortedPairs.Count) | |
{ | |
keepGoing = false; | |
} | |
else | |
{ | |
keepGoing = (sortedPairs[currentIndex].Value != 0); | |
} | |
} | |
var afterCombinedCount = sortedPairs.Where(sp => sp.Value != 0).ToList(); | |
// var desc = afterCombinedCount.Aggregate("", (acc, next) => | |
// { | |
// var key = next.Key; | |
// var v = next.Value; | |
// var combined = acc + "\n" + key.ToString() + ": " + v.ToString(); | |
// return combined; | |
// }); | |
// Console.WriteLine(desc); | |
// Write an action using Console.WriteLine() | |
// To debug: Console.Error.WriteLine("Debug messages..."); | |
Console.WriteLine(afterCombinedCount.Count.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment