Esempio n. 1
0
        public static void Main()
        {
            var number = int.Parse(Console.ReadLine());

            var legions = new List <Legion>();

            for (int i = 0; i < number; i++)
            {
                var line = Console.ReadLine()
                           .Split(new[] { "=", "-", ">", ":", " " }, StringSplitOptions.RemoveEmptyEntries)
                           .ToArray();

                var lastActivity = int.Parse(line[0]);
                var legionName   = line[1];
                var soldierType  = line[2];
                var soldierCount = int.Parse(line[3]);

                if (!legions.Any(n => n.Name == legionName))
                {
                    var newLegion = new Legion();
                    newLegion.Name         = legionName;
                    newLegion.LastActivity = lastActivity;
                    newLegion.Soldier      = new Dictionary <string, long>();
                    newLegion.Soldier.Add(soldierType, soldierCount);

                    legions.Add(newLegion);
                }
                else if (legions.Any(n => n.Name == legionName))
                {
                    var legionsIndex = legions.FindIndex(n => n.Name == legionName);
                    if (!legions[legionsIndex].Soldier.Any(n => n.Key == soldierType))
                    {
                        legions[legionsIndex].Soldier.Add(soldierType, soldierCount);

                        if (legions[legionsIndex].LastActivity < lastActivity)
                        {
                            legions[legionsIndex].LastActivity = lastActivity;
                        }
                    }
                    else
                    {
                        legions[legionsIndex].Soldier[soldierType] += soldierCount;

                        if (legions[legionsIndex].LastActivity < lastActivity)
                        {
                            legions[legionsIndex].LastActivity = lastActivity;
                        }
                    }
                }
            }

            var outputCondition = Console.ReadLine()
                                  .Split('\\')
                                  .ToArray();

            int  digit;
            var  firstChar = outputCondition[0].First().ToString();
            bool condition = int.TryParse(firstChar, out digit);

            if (condition)
            {
                var soldierType    = outputCondition[1];
                var borderActivity = int.Parse(outputCondition[0]);

                foreach (var legion in legions
                         .Where(s => s.Soldier.ContainsKey(soldierType))
                         .Where(a => a.LastActivity < borderActivity)
                         .OrderByDescending(s => s.Soldier[soldierType]))
                {
                    Console.WriteLine($"{legion.Name} -> {legion.Soldier[soldierType]}");
                }
            }
            else
            {
                foreach (var legion in legions.OrderByDescending(a => a.LastActivity))
                {
                    Console.WriteLine($"{legion.LastActivity} : {legion.Name}");
                }
            }
        }
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            Dictionary <string, Legion> legions = new Dictionary <string, Legion>();

            for (int i = 0; i < n; i++)
            {
                string[] input        = Console.ReadLine().Split(new string[] { " = ", " -> ", ":" }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                int      lastActivity = int.Parse(input[0]);
                string   legionName   = input[1];
                string   soldierType  = input[2];
                int      soldierCount = int.Parse(input[3]);


                if (legions.ContainsKey(legionName))
                {
                    //update
                    //IN BOTH cases, stated above, you should update the last activity, with the newly entered one,
                    //ONLY if the entered one is GREATER than the previous one.
                    if (legions[legionName].LastActivity < lastActivity)
                    {
                        legions[legionName].LastActivity = lastActivity;
                    }
                    //If a given legion already exists, you must add the new soldier type, with its count.
                    //If the soldier type exists ALSO, you should just add the soldier count.
                    if (legions[legionName].Soldiers.ContainsKey(soldierType))
                    {
                        legions[legionName].Soldiers[soldierType] += soldierCount;
                    }
                    else
                    {
                        legions[legionName].Soldiers.Add(soldierType, soldierCount);
                    }
                }
                else
                {
                    //add
                    Legion legion = new Legion();
                    legion.LegionName   = legionName;
                    legion.LastActivity = lastActivity;
                    Dictionary <string, long> tmp = new Dictionary <string, long>();
                    tmp.Add(soldierType, soldierCount);
                    legion.Soldiers = tmp;
                    legions.Add(legionName, legion);
                }
            }

            string[] command = Console.ReadLine().Split('\\').ToArray();
            if (command.Length == 1)
            {
                //{lastActivity} : {legionName}
                //you must print all legions which have the given soldier type, with last activity, and legion name.
                //The legions must be printed in descending order of their activity.
                var leg = legions.Where(x => x.Value.Soldiers.Keys.Contains(command[0])).OrderByDescending(x => x.Value.LastActivity);

                if (leg.Count() != 0)
                {
                    foreach (var legionsPair in leg)
                    {
                        Console.WriteLine($"{legionsPair.Value.LastActivity} : {legionsPair.Key}");
                    }
                }
            }
            else
            {
                //input  - {activity}\{soldierType}
                //{legionName} -> {soldierCount}
                //In the first case, you must print all legions, and the count of soldiers they have from the given soldier type,
                //who’s last activity is LOWER than the given activity. The legions must be printed in descending order by soldier count.

                var leg = legions.Where(x => x.Value.LastActivity < int.Parse(command[0]) && x.Value.Soldiers.ContainsKey(command[1]))
                          .OrderByDescending(x => x.Value.Soldiers[command[1]]);

                if (leg.Count() != 0)
                {
                    foreach (var item in leg)
                    {
                        Console.WriteLine($"{item.Key} -> {item.Value.Soldiers[command[1]]}");
                    }
                }
            }
        }