Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 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 PracticeEx2
  8. {
  9. class Program
  10. {
  11. static void ShowList(List<int[]> list)
  12. {
  13. foreach (int[] elem in list)
  14. {
  15. foreach (int el in elem)
  16. {
  17. Console.Write(el + " ");
  18. }
  19. Console.Write("\n");
  20. }
  21. }
  22. static List<int[]> Reverse(List<int[]> list)
  23. {
  24. foreach (int[] elem in list)
  25. {
  26. int a = elem[0];
  27. elem[0] = elem[1];
  28. elem[1] = a;
  29. }
  30. return list;
  31. }
  32. static int BinarySearch(int left, int right, int value, List<int[]> list)
  33. {
  34. while (left < right)
  35. {
  36. int middle = left + (right - 1) / 2;
  37. if (list[middle][1] - value <= 0)
  38. {
  39. left = middle + 1;
  40. }
  41. else
  42. right = middle;
  43. }
  44. return
  45. right;
  46. }
  47. static void Main(string[] args)
  48. {
  49. string[] str = Console.ReadLine().Split(' ');
  50. int[] nd = new int[str.Length];
  51. for (int i = 0; i < nd.Length; i++)
  52. {
  53. nd[i] = Convert.ToInt32(str[i]);
  54. }
  55. int average = nd[1];
  56. List<int[]> inputList = new List<int[]>();
  57. List<int[]> outputList = new List<int[]>(nd[0]);
  58. int[] arr = new int[2];
  59. for (int i = 0; i < nd[0]; ++i)
  60. {
  61. int curd;
  62. curd = Convert.ToInt32(Console.ReadLine());
  63. if (curd != average)
  64. {
  65. arr[0] = i + 1;
  66. arr[1] = curd;
  67. inputList.Add(arr);
  68. }
  69. }
  70. var sortedList = inputList.OrderBy(x => x[1]).ToList();
  71. for (int i = 0; i < sortedList.Count; ++i)
  72. {
  73. if (sortedList[i][1] == average)
  74. break;
  75. else
  76. {
  77. int ind = BinarySearch(i + 1, sortedList.Count, average - sortedList[i][1], sortedList);
  78. arr[0] = sortedList[ind][0];
  79. arr[1] = average - sortedList[i][1];
  80. outputList[sortedList[i][0] - 1] = arr;
  81. sortedList[ind][1] -= average - sortedList[i][1];
  82. sortedList[i][1] = average;
  83. }
  84. }
  85. ShowList(outputList);
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement