Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PracticeEx2
- {
- class Program
- {
- static void ShowList(List<int[]> list)
- {
- foreach (int[] elem in list)
- {
- foreach (int el in elem)
- {
- Console.Write(el + " ");
- }
- Console.Write("\n");
- }
- }
- static List<int[]> Reverse(List<int[]> list)
- {
- foreach (int[] elem in list)
- {
- int a = elem[0];
- elem[0] = elem[1];
- elem[1] = a;
- }
- return list;
- }
- static int BinarySearch(int left, int right, int value, List<int[]> list)
- {
- while (left < right)
- {
- int middle = left + (right - 1) / 2;
- if (list[middle][1] - value <= 0)
- {
- left = middle + 1;
- }
- else
- right = middle;
- }
- return
- right;
- }
- static void Main(string[] args)
- {
- string[] str = Console.ReadLine().Split(' ');
- int[] nd = new int[str.Length];
- for (int i = 0; i < nd.Length; i++)
- {
- nd[i] = Convert.ToInt32(str[i]);
- }
- int average = nd[1];
- List<int[]> inputList = new List<int[]>();
- List<int[]> outputList = new List<int[]>(nd[0]);
- int[] arr = new int[2];
- for (int i = 0; i < nd[0]; ++i)
- {
- int curd;
- curd = Convert.ToInt32(Console.ReadLine());
- if (curd != average)
- {
- arr[0] = i + 1;
- arr[1] = curd;
- inputList.Add(arr);
- }
- }
- var sortedList = inputList.OrderBy(x => x[1]).ToList();
- for (int i = 0; i < sortedList.Count; ++i)
- {
- if (sortedList[i][1] == average)
- break;
- else
- {
- int ind = BinarySearch(i + 1, sortedList.Count, average - sortedList[i][1], sortedList);
- arr[0] = sortedList[ind][0];
- arr[1] = average - sortedList[i][1];
- outputList[sortedList[i][0] - 1] = arr;
- sortedList[ind][1] -= average - sortedList[i][1];
- sortedList[i][1] = average;
- }
- }
- ShowList(outputList);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement