SHARE
TWEET

Untitled

a guest Jul 19th, 2017 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02.Processor_Scheduling
  6. {
  7.     internal class Program
  8.     {
  9.         private static void Main()
  10.         {
  11.             var tasksCount = int.Parse(
  12.                 Console.ReadLine()
  13.                 .Split(':')[1]
  14.                 .Trim());
  15.  
  16.             var tasks = new List<List<int>>();
  17.            
  18.             for (var i = 1; i <= tasksCount; i++)
  19.             {// 0 - Value, 1 - Deadline, 2 - Index
  20.                 tasks.Add(Console.ReadLine()
  21.                     .Split('-')
  22.                     .Select(x => int.Parse(x.Trim()))
  23.                     .ToList());
  24.  
  25.                 tasks[i - 1].Add(i);
  26.             }
  27.  
  28.             tasks = tasks
  29.                 .OrderByDescending(task => task[0])
  30.                 .ThenBy(task => task[1])
  31.                 .ToList();
  32.  
  33.             var maxDeadLine = tasks.Max(x => x[1]);
  34.  
  35.             var slots = new bool[maxDeadLine];
  36.  
  37.             var result = new List<List<int>>();
  38.  
  39.             foreach (var task in tasks)
  40.             {
  41.                 for (var i = task[1] - 1; i >= 0; i--)
  42.                 {
  43.                     if (slots[i])
  44.                     {
  45.                         continue;
  46.                     }
  47.  
  48.                     slots[i] = true;
  49.                     result.Add(task);
  50.                     break;
  51.                 }
  52.             }
  53.  
  54.             var schedule = result
  55.                 .OrderBy(x => x[1])
  56.                 .ThenByDescending(x => x[0])
  57.                 .Select(x => x[2])
  58.                 .ToList();
  59.  
  60.             Console.WriteLine($"Optimal schedule: {string.Join(" -> ", schedule)}");
  61.             Console.WriteLine($"Total value: {result.Sum(x => x[0])}");
  62.         }
  63.     }
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top