14906 - Magical Evaluation   

Description

In the Continental Magic Association, n mages are undergoing the First-Class Mage Exam. Each mage has a mana value represented by an integer ranging from -99 to 99. The Great Mage Serie, intending to prevent Frieren from passing easily, wants to evaluate the mages from multiple complex perspectives and will issue q queries. Each query specifies a different sorting rule.

Your task is to sort the mages based on the rule specified in the query. If a tie occurs under any rule, you should resolve it by sorting the mages' names in ascending lexicographical (alphabetical) order.

The q queries will be represented by an integer rule (from 0 to 4), corresponding to the following sorting rules:

  • Rule 0 (Ascending): Sort mages by their power value in ascending order.

  • Rule 1 (Square Ascending): Sort mages by the square of their power value in ascending order.

  • Rule 2 (Digit Sum Ascending): Sort mages by the sum of their power value's digits in ascending order (ignoring the negative sign). For example, the digit sum of -85 is 8 + 5 = 13.

  • Rule 3 (Swapped Digit Ascending): Swap the tens digit and the units digit of the power value, and sort in ascending order. For example, 78 becomes 87, -6 becomes -60.

  • Rule 4 (Prime First Ascending): Take the absolute value of each mage's power value. If this absolute value is a prime number (e.g., 2, 3, 5...), the mage must be placed at the front. The remaining mages are placed behind. Within the prime and non-prime groups, sort them by their original power value in ascending order.

Note: This is a partial judge problem. You need to implement a total of 6 functions.
Please refer to the provided .h header file for detailed specifications.

Input

The first line contains two integers n and q, representing the number of mages and the number of queries.

The next n lines each contain a string name and their value.

The next q lines each contain a single integer rule, representing the sorting rule for that query.

Constraints:

  • 1 <= n, q <= 100

  • len(name) < 30

  • -99 <= value <= 99

Testcase Design:

  • Testcase 1~2: Rule 0, 1
  • Testcase 3~4: Rule 0, 1, 2
  • Testcase 5~8: Rule 0, 1, 2, 3, 4

Output

For each query, output the sorted names of the mages.

Remember to add '\n' at the end of every line.

 

Sample Input  Download

Sample Output  Download

Partial Judge Code

14906.c

Partial Judge Header

14906.h

Tags




Discuss