Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         int n = int.Parse(Console.ReadLine().Split(' ').ToArray()[1]);
  10.         List<Task> tasks = new List<Task>();
  11.         int maxDeadline = 0;
  12.         for (int i = 0; i < n; i++)
  13.         {
  14.             int[] given = Console.ReadLine().Split(new string[] { " - " }, StringSplitOptions.RemoveEmptyEntries)
  15.                 .Select(int.Parse).ToArray();
  16.             int value = given[0];
  17.             int deadline = given[1];
  18.             if (maxDeadline < deadline)
  19.             {
  20.                 maxDeadline = deadline;
  21.             }
  22.             tasks.Add(new Task(i + 1, value, deadline));
  23.         }
  24.         tasks.Sort();
  25.         int totalValue = 0;
  26.         List<Task> executed = new List<Task>();
  27.         foreach (var task in tasks)
  28.         {
  29.             if (maxDeadline > executed.Count)
  30.             {
  31.                 totalValue += task.Value;
  32.                 executed.Add(task);
  33.             }
  34.         }
  35.         executed = executed.OrderBy(task => task.Deadline).ToList();
  36.         Console.WriteLine("Optimal schedule: " + string.Join(" -> ", executed));
  37.         Console.WriteLine("Total value: " + totalValue);
  38.     }
  39.  
  40.     class Task : IComparable<Task>
  41.     {
  42.         public int TaskNum { get; }
  43.         public int Value { get; }
  44.         public int Deadline { get; }
  45.  
  46.         public Task(int taskNum, int value, int deadline)
  47.         {
  48.             this.TaskNum = taskNum;
  49.             this.Value = value;
  50.             this.Deadline = deadline;
  51.         }
  52.         public int CompareTo(Task other)
  53.         {
  54.             int cmp = other.Value.CompareTo(this.Value);
  55.             if (cmp == 0)
  56.                 cmp = other.Deadline.CompareTo(this.Deadline);
  57.  
  58.             return cmp;
  59.         }
  60.  
  61.         public override string ToString()
  62.         {
  63.             return TaskNum.ToString();
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement