Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6.  
  7. namespace Contest1
  8. {
  9. class Program
  10. {
  11. //private static int[] costs;
  12. //private static int n, k;
  13.  
  14. private static int[] costs = { 2, -3, 5 };
  15. private static int n = 5;
  16. private static int k = 3;
  17.  
  18.  
  19. static void Main(string[] args)
  20. {
  21. GetData();
  22. GetAnswer();
  23. }
  24.  
  25. private static void GetData()
  26. {
  27. using (StreamReader sr = new StreamReader("input.txt"))
  28. {
  29. string[] constants = sr.ReadLine().Split(' ');
  30. int.TryParse(constants[0], out n);
  31. int.TryParse(constants[1], out k);
  32. costs = sr.ReadLine().Replace("\n", "").Split(' ').Select(x => int.Parse(x)).ToArray();
  33. }
  34. }
  35.  
  36. static void GetAnswer()
  37. {
  38. List<int> jumps = new List<int>() { 0, 0 };
  39. List<int> results = new List<int>() { 0, 0 };
  40. for (int i = 2; i <= n; i++)
  41. {
  42.  
  43. Tuple<int, int> best_variant = new Tuple<int, int>(i - 1, results[i - 1]);
  44. int j = 1;
  45. while (j <= k && i - j > 0)
  46. {
  47. if (results[i - j] > best_variant.Item2)
  48. {
  49. best_variant = new Tuple<int, int>(i - j, results[i - j]);
  50. }
  51.  
  52. j++;
  53. }
  54.  
  55. jumps.Add(best_variant.Item1);
  56. results.Add(i - 2 < costs.Length ? best_variant.Item2 + costs[i - 2] : best_variant.Item2);
  57.  
  58.  
  59. }
  60.  
  61.  
  62. int counter = 0;
  63. int l = results.Count - 1;
  64. List<int> way = new List<int>() { l };
  65.  
  66. while (l > 1)
  67. {
  68. l = jumps[l];
  69. way.Add(l);
  70. counter++;
  71. }
  72. way.Reverse();
  73.  
  74.  
  75. using (StreamWriter sw = new StreamWriter("output.txt"))
  76. {
  77. sw.WriteLine(results.Last());
  78. sw.WriteLine(counter);
  79. sw.WriteLine(string.Join(" ", way));
  80. }
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement