Advertisement
Galin_P87

MoreEx Dictionaries - 01. Ranking

Dec 3rd, 2018
1,185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _01._Ranking
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             string firstLine = Console.ReadLine();
  12.             Dictionary<string, string> contestPasswords = new Dictionary<string, string>();
  13.  
  14.             while (firstLine != "end of contests")
  15.             {
  16.                 string[] tokens = firstLine.Split(':');
  17.                 string contest = tokens[0];
  18.                 string passwordForContest = tokens[1];
  19.                 contestPasswords.Add(contest, passwordForContest);
  20.                 firstLine = Console.ReadLine();
  21.             }
  22.  
  23.             string secondLine = Console.ReadLine();
  24.             SortedDictionary<string, Dictionary<string, int>> submissions = new SortedDictionary<string, Dictionary<string, int>>();
  25.  
  26.             while (secondLine != "end of submissions")
  27.             {
  28.                 string[] tokens = secondLine.Split("=>");
  29.                 string contest = tokens[0];
  30.                 string password = tokens[1];
  31.                 string username = tokens[2];
  32.                 int points = int.Parse(tokens[3]);
  33.  
  34.                 if (!contestPasswords.ContainsKey(contest)
  35.                     || contestPasswords[contest] != password)
  36.                 {
  37.                     secondLine = Console.ReadLine();
  38.                     continue;
  39.                 }
  40.  
  41.                 if (!submissions.ContainsKey(username))
  42.                 {
  43.                         submissions[username] = new Dictionary<string, int> { { contest, points } };
  44.                 }
  45.  
  46.                 if (!submissions[username].ContainsKey(contest))
  47.                 {
  48.                     submissions[username].Add(contest, points);
  49.                 }
  50.  
  51.                 if (submissions[username][contest] < points)
  52.                 {
  53.                     submissions[username][contest] = points;
  54.                 }
  55.  
  56.                 secondLine = Console.ReadLine();
  57.             }
  58.  
  59.             Dictionary<string, int> usernameTotalPoints = new Dictionary<string, int>();
  60.             foreach (var kvp in submissions)
  61.             {
  62.                 usernameTotalPoints[kvp.Key] = kvp.Value.Values.Sum();
  63.             }
  64.             string bestCandidate = usernameTotalPoints
  65.                 .Keys
  66.                 .Max();
  67.             int bestPoints = usernameTotalPoints
  68.                 .Values
  69.                 .Max();
  70.  
  71.             Console.WriteLine($"Best candidate is {bestCandidate} with total {bestPoints} points.");
  72.  
  73.             Console.WriteLine("Ranking:");
  74.             foreach (var kvp in submissions)
  75.             {
  76.                 Console.WriteLine(kvp.Key);
  77.                 Console.WriteLine(string.Join(Environment.NewLine, kvp.Value
  78.                     .OrderByDescending(x => x.Value)
  79.                     .Select(a => $"#  {a.Key} -> {a.Value}")
  80.                     ));
  81.             }          
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement