Я застрял в проблеме, и мне нужна помощь от ярких умов SO. У меня есть N пар целых чисел без знака. Мне нужно их сортировать. Конечный вектор пар следует сортировать неубывающим образом по первому числу в каждой паре и невозмутимо вторым в каждой паре. Каждая пара может иметь первый и второй элементы, обмениваемые друг с другом. Иногда нет решения, поэтому мне нужно сделать исключение.
Пример:
in pairs:
1 5
7 1
3 8
5 6
out pairs:
1 7 <-- swapped
1 5
6 5 <-- swapped
8 3 <-- swapped
^ ^ Без перестановки пар невозможно построить решение. Итак, мы поменяем пары (7, 1), (3, 8) и (5, 6) и построим результат. или
in pairs:
1 5
6 9
out:
not possible
Еще один пример, показывающий, как "сортировка пар" сначала не является решением.
in pairs:
1 4
2 5
out pairs:
1 4
5 2
Спасибо