GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Wednesday 20 July 2022

[Solution] Strict Permutation CodeChef Solution



You are given an integer N. You have to find a permutation P of the integers {1,2,,N} that satisfies M conditions of the following form:

  • (Xi,Yi), denoting that the element Xi(1XiN) must appear in the prefix of length Yi. Formally, if the index of the element Xi is Ki (i.e, PKi=Xi), then the condition 1KiYi must hold.

Print -1 if no such permutation exists. In case multiple permutations that satisfy all the conditions exist, find the lexicographically smallest one.

Note: If two arrays A and B have the same length N, then A is called lexicographically smaller

Solution Click Below:-  👉CLICK HERE👈
👇👇👇👇👇

 than B only if there exists an index i(1iN) such that A1=B1, A2=B2, , Ai1=Bi1,Ai<Bi.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.

  • The first line of each test case contains two space-separated integers N and M — the length of permutation and the number of conditions to be satisfied respectively.
  • The next M lines describe the conditions. The i-th of these M lines contains two space-separated integers Xi and Yi.

Output Format

For each test case, output a single line containing the answer:

  • If no permutation satisfies the given conditions, print −1.
  • Otherwise, print N space-separated integers, denoting the elements of the permutation. If there are multiple answers, output the lexicographically smallest one.

Constraints

  • 1T105
  • 1N105
  • 1MN
  • 1Xi,YiN
  • XiXj for each 1i<jM.
  • The sum of N over all test cases won't exceed 2105.

No comments:

Post a Comment