chronologic-counterexample

See Russian Translation

Carol wants to sort an array of n integers. The array contains a permutation of {1,2,..,n}. 

Carol can only sort subarrays at a time. More specifically, she has a list of m subarrays in order that she will sort by. A subarray is a contiguous portion of the original array, which can be specified by a start and end index. 

She wonders if the list she has will sort every permutation correctly. If so, print the phrase "All ok!". If not, determine the lexicographically smallest permutation that will not be correctly sorted.

Input format:

The first line of input will contain two integers n,m.
The following m lines of input will contain two integers li,ri describing the start and end indices of the subarray that Carol will sort.

Output format:

If every permutation is correctly sorted, print the string "All ok!" (without quotes). Otherwise, print n space separated integers, representing the lexicographically smallest permutation of {1,2,..,n} that can't be sorted. 

Constraints:

For all subtasks
1 ≤ li ≤ ri ≤ n

40 pts:
1 ≤ n ≤ 10
1 ≤ m ≤ 3

60 pts:
1 ≤ n ≤ 50
1 ≤ m ≤ 50

SAMPLE INPUT
9 2
1 6
4 9

SAMPLE OUTPUT
1 2 4 5 6 7 3 8 9

Explanation

In this case, Carol has a list of 9 integers, and she sorts the subarray from the 1st to 6th element, then the 4rd to 9th element. We can see that this will not correctly sort the permutation {1,2,4,5,6,7,3,8,9}, since the first subarray will make the array become {1,2,4,5,6,7,3,8,9}, and the second will make it {1,2,4,3,5,6,7,8,9}, which is not in sorted order. This is also the lexicographically smallest permutation that can't be sorted.

Source: hackerearth | Difficulty: EASY
Tags: Greedy algorithms


Solutions: