TheBulgarianWolf

Ranking

Mar 10th, 2021
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Ranking
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string input;
  12.             Dictionary<string, string> passwords = new Dictionary<string, string>();
  13.             var collection = new Dictionary<string, Dictionary<string, int>>();
  14.             while ((input = Console.ReadLine()) != "end of contests")
  15.             {
  16.                 string[] arr = input.Split(":");
  17.                 string contest = arr[0];
  18.                 string pass = arr[1];
  19.                 passwords.Add(contest, pass);
  20.             }
  21.             string secondInput;
  22.             while ((secondInput = Console.ReadLine()) != "end of submissions")
  23.             {
  24.                 string[] secArr = secondInput.Split("=>");
  25.                 string contest = secArr[0];
  26.                 string pass = secArr[1];
  27.                 string name = secArr[2];
  28.                 int points = int.Parse(secArr[3]);
  29.                 if (passwords.ContainsKey(contest))
  30.                 {
  31.                     if(passwords[contest] == pass)
  32.                     {
  33.                         if (collection.ContainsKey(name))
  34.                         {
  35.                             if (collection[name].ContainsKey(contest))
  36.                             {
  37.                                 if(collection[name][contest] < points)
  38.                                 {
  39.                                     collection[name][contest] = points;
  40.                                 }
  41.                             }
  42.                             else
  43.                             {
  44.                                 collection[name].Add(contest, points);
  45.                             }
  46.                         }
  47.                         else
  48.                         {
  49.                             collection.Add(name, new Dictionary<string, int>());
  50.                             collection[name].Add(contest, points);
  51.                         }                      
  52.                     }
  53.                 }
  54.             }
  55.  
  56.            
  57.             int sum = 0;
  58.             int maxSum = 0;
  59.             string bestCandidate = "";
  60.             var col = collection;
  61.             foreach(var user in collection.Keys)
  62.             {
  63.                 foreach(var contest in collection[user])
  64.                 {
  65.                     sum += contest.Value;
  66.                 }
  67.                 if(sum > maxSum)
  68.                 {
  69.                     maxSum = sum;
  70.                     bestCandidate = user;
  71.                     sum = 0;
  72.                 }
  73.                
  74.                
  75.             }
  76.             var orderedCollection = collection.OrderBy(n => n.Key).ThenByDescending(v => v.Value).ToDictionary(k => k.Key,v => v.Value);
  77.             Console.WriteLine($"Best candidate is {bestCandidate} with total {maxSum} points.");
  78.             Console.WriteLine("Ranking: ");
  79.             foreach (var user in orderedCollection.Keys)
  80.             {
  81.                 Console.WriteLine(user);
  82.                 foreach(var contest in collection[user])
  83.                 {
  84.                     Console.WriteLine("# " + contest.Key + " -> " + contest.Value);
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
  90.  
Add Comment
Please, Sign In to add comment