Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Hashcode
- {
- class Program
- {
- static void Main(string[] args)
- {
- StringBuilder sb = new StringBuilder();
- var booksIds = new List<int>();
- //First id of the library second list is the number of books and the time for shiping
- var librariesInformation = new Dictionary<int, List<int>>();
- var librariesBooks = new Dictionary<int, List<int>>();
- var librariesScanTimeLeft = new Dictionary<int, int>();
- var librariesResults = new Dictionary<int, List<int>>();
- string text = System.IO.File.ReadAllText(@"C:\Users\Ilia\source\repos\Hashcode\b_read_on.txt");
- string[] inputs = text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
- string[] firstLine = inputs[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
- int numberOfBooks = int.Parse(firstLine[0]);
- int numberOfLibraries = int.Parse(firstLine[1]);
- int daysToScan = int.Parse(firstLine[2]);
- string[] secondLine = inputs[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
- foreach (var item in secondLine)
- {
- booksIds.Add(int.Parse(item));
- }
- for (int i = 2; i < inputs.Length; i++)
- {
- int currentLibraryId = i - 2;
- string[] libraryInfo = inputs[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
- if (i % 2 == 0)
- {
- if (currentLibraryId > 1)
- {
- currentLibraryId -= 1;
- }
- int numberOfBooksForCurrentLibrary = int.Parse(libraryInfo[0]);
- int sighupProcesDuration = int.Parse(libraryInfo[1]);
- int canShipNumberOfBooks = int.Parse(libraryInfo[2]);
- librariesInformation.Add(currentLibraryId, new List<int>());
- librariesInformation[currentLibraryId].Add(numberOfBooksForCurrentLibrary);
- librariesInformation[currentLibraryId].Add(sighupProcesDuration);
- librariesInformation[currentLibraryId].Add(canShipNumberOfBooks);
- }
- else
- {
- if (currentLibraryId > 1)
- {
- currentLibraryId -= 1;
- }
- librariesBooks.Add(currentLibraryId - 1, new List<int>());
- foreach (var item in libraryInfo)
- {
- int index = int.Parse(item);
- if (librariesBooks[currentLibraryId - 1].Any(x => x == index))
- {
- continue;
- }
- librariesBooks[currentLibraryId - 1].Add(index);
- }
- }
- }
- librariesInformation = librariesInformation.OrderByDescending(x => x.Value[1]).ToDictionary(k => k.Key, k => k.Value);
- int tempDaysToScan = daysToScan -= librariesInformation[librariesInformation.FirstOrDefault().Key][1];
- librariesScanTimeLeft.Add(librariesInformation.FirstOrDefault().Key, tempDaysToScan);
- foreach (var item in librariesInformation)
- {
- if (tempDaysToScan - item.Value[1] < 0)
- {
- break;
- }
- if (librariesScanTimeLeft.ContainsKey(item.Key))
- {
- continue;
- }
- tempDaysToScan -= item.Value[1];
- librariesScanTimeLeft.Add(item.Key, tempDaysToScan);
- }
- foreach (var item in librariesScanTimeLeft)
- {
- int howManyBooksCanBeScan = item.Value * librariesInformation[item.Key][2];
- librariesResults.Add(item.Key, new List<int>());
- foreach (var book in librariesBooks[item.Key])
- {
- if (howManyBooksCanBeScan <= 0)
- {
- break;
- }
- librariesResults[item.Key].Add(book);
- }
- }
- sb.Append(librariesResults.Count + "\r\n");
- foreach (var item in librariesResults)
- {
- sb.Append(item.Key + " " + item.Value.Count + "\r\n");
- foreach (var book in item.Value)
- {
- sb.Append(book + " ");
- }
- sb.Append("\r\n");
- }
- //foreach (var key in librariesInformation)
- //{
- // Console.WriteLine("Library id: " + key.Key);
- // foreach (var item in key.Value)
- // {
- // Console.Write(item);
- // }
- // Console.WriteLine();
- // Console.WriteLine("Books: ");
- // foreach (var value in librariesBooks[key.Key])
- // {
- // Console.Write(value);
- // }
- // Console.WriteLine();
- //}
- System.IO.File.WriteAllText(@"C:\Users\Ilia\source\repos\Hashcode\Hashcode\ResultText.txt", sb.ToString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement