#P5262. Optimizing JYY's Position in the Lineup

    ID: 18498 Type: Default 1000ms 256MiB

Optimizing JYY's Position in the Lineup

Optimizing JYY's Position in the Lineup

There are N students in a physical education class with IDs from 1 to N. The student JYY has ID P. The height of the student with ID i is given by \(h_i\). Initially, the students stand in increasing order of their IDs (from left to right).

The teacher, Mr. KFC, uses a peculiar method to form a queue. He will perform a series of exactly N-1 comparisons as follows: for each round \(i\) from 1 to \(N-1\), he goes to the student at position \(i\) (positions are 1-indexed) and compares that student with the student immediately to his right (position \(i+1\)). If the height of the left student is greater than that of the right (i.e. if \(h_{left} > h_{right}\)), he swaps their positions.

JYY, being eager to get a better (more leftward) position (since the teacher will later choose the rightmost students to move tables), has a special trick. When the teacher is about to perform a comparison that involves him, he may choose to crouch down to tie his shoelace. If he does so, then in that particular round the teacher skips the height comparison for the pair that involves him – that is, no swap occurs between JYY and his adjacent student for that round.

However, crouching takes energy. JYY may crouch at most K times. A strategy is represented by a string of length \(N-1\) where the \(i\)th character is:

  • '+' if, in the \(i\)th round, JYY chooses to crouch (if he is involved in the comparison), and
  • '-' if he does not.

Important: JYY can only affect the comparison if he is the left student in the pair. When he is the right student, he does not get a choice, and the swap (if warranted) is performed normally.

The effect of the swap is as follows:

  • If JYY is the right student in a pair and the left student is taller than him (\(h_{left} > h_{JYY}\)), then a swap occurs and JYY moves one position to the left (which is beneficial).
  • If JYY is the left student and his height is greater than that of his right neighbor (\(h_{JYY} > h_{right}\)), then a swap would take him one position to the right (which is harmful) – so in this case he may choose to crouch (if he still has any crouch chances available) to avoid the swap.

Your task is to determine an optimal strategy (i.e. a string of length \(N-1\) consisting of '+' and '-' with at most K plus signs) that minimizes JYY's final position (making him as far left as possible) after the teacher has performed all \(N-1\) rounds. Note that if a round does not involve JYY, the corresponding character in the strategy does not affect the outcome and should be '-'.

Input/Output Example Explanation:

For example, suppose we have \(N = 5\), \(P = 3\), \(K = 1\) and the heights are given as follows:

(h_1=160,; h_2=170,; h_3=180,; h_4=165,; h_5=150).

Initial lineup (positions in order):

  1. Student 1 (height 160)
  2. Student 2 (height 170)
  3. Student 3 (JYY, height 180)
  4. Student 4 (height 165)
  5. Student 5 (height 150)

Consider the rounds:

  • Round 1 (i = 1): Compare positions 1 and 2: 160 and 170. No swap needed. (Strategy character: '-'.)
  • Round 2 (i = 2): Compare positions 2 and 3: 170 and 180. JYY is at position 3 (right) so no decision is available; 170 < 180, so no swap. (Strategy character: '-'.)
  • Round 3 (i = 3): Compare positions 3 and 4: Now JYY is at position 3 and is the left student. Since 180 > 165, a swap would normally occur (moving JYY to position 4), but he can choose to crouch to skip this comparison. With one crouch available, he chooses to crouch here. (Strategy character: '+'.)
  • Round 4 (i = 4): Compare positions 4 and 5. JYY is not involved. (Strategy character: '-'.)

Thus, the optimal strategy string is "--+-", and the final position of JYY is improved.

inputFormat

The input consists of two lines:

  • The first line contains three integers \(N\), \(P\), and \(K\) separated by spaces. \(N\) (\(2 \leq N \leq 10^5\)) is the number of students, \(P\) (\(1 \leq P \leq N\)) is the ID of JYY, and \(K\) (\(0 \leq K \leq N\)) is the maximum number of times JYY can crouch.
  • The second line contains \(N\) integers \(h_1, h_2, \ldots, h_N\), where \(h_i\) represents the height of the student with ID \(i\). The heights are positive integers.

outputFormat

Output a string of length \(N-1\) consisting of characters '+' and '-'. This string represents the optimal crouching strategy so that JYY achieves the leftmost possible final position in the queue. The string must contain at most \(K\) plus signs.

sample

5 3 1
160 170 180 165 150
--+-