Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Proccesor_Schedualing
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string input = Console.ReadLine();
  14. int colonIndex = input.IndexOf(":");
  15. int n = int.Parse(input.Substring(colonIndex + 1).Trim());
  16. var tasks = new List<KeyValuePair<int, Tuple<int,int>>>();
  17. for (int i = 0; i < n; i++)
  18. {
  19. int[] entry = Console.ReadLine().Split(new char[] { ' ', '-' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  20. tasks.Add(new KeyValuePair<int, Tuple<int,int>>(entry[0], new Tuple<int,int>(entry[1],i+1)));
  21. }
  22. Schedualing(tasks);
  23. }
  24.  
  25. public static void Schedualing(List<KeyValuePair<int, Tuple<int, int>>> task)
  26. {
  27. int stepsCount = task.OrderByDescending(x => x.Value.Item1).FirstOrDefault().Value.Item1;
  28. var sorted = task.OrderByDescending(x => x.Key).ThenByDescending(x=>x.Value.Item1);
  29. var top = sorted.Take(stepsCount).OrderBy(x=>x.Value.Item1).ToList();
  30. var result = new List<int>();
  31. int[] steps = new int[stepsCount];
  32. int skip = 0;
  33. for (int i = 0; i < stepsCount; i++)
  34. {
  35. var item = top.Skip(skip).Take(1).FirstOrDefault();
  36. result.Add(item.Key);
  37. steps[i] = item.Value.Item2;
  38. skip++;
  39. }
  40. Console.WriteLine("Optimal schedule: "+string.Join(" -> ",steps));
  41. int totalVal = result.Sum();
  42. Console.WriteLine("Total value: "+totalVal );
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement