report VI VI EN
Register | Login
  • HOME
  • PROBLEMSET
  • ROADMAP
  • COMPETITION
  • TOPIC
  • RANKING
  • GUIDE
  • MASHUP
  • ABOUT
  • CONTACT
  • Problem
  • Submit
  • Results
Bubble sort - FlandreOJ: Flandre Online Judge

Bubble sort

Time limit: 2000 ms
Memory limit: 256 MB
**Description:** Given an array $a$ of length $n$ and a string $S$ consisting of characters 'L' and 'R'. - If the character is 'L', we perform one pass of Bubble Sort from left to right. - If the character is 'R', we perform one pass of Bubble Sort from right to left. **Task**: Find the minimum prefix length of $S$ required to sort the array $a$. If the array is not sorted even after performing all operations in $S$, output -1. #### Input - First line: $n$ ($1 \le n \le 5 \times 10^5$). - Second line: $n$ integers $a_1, \dots, a_n$. - Third line: String $S$. **Constraints:** - $1 \le n \le 5 \times 10^5$. - Length of $S$ is at least 1. - Elements of $a$ are distinct (or rank-equivalent). #### Output - Print the minimum number of operations. Output -1 if impossible. #### Example Input: ``` 5 2 3 4 5 1 LRL ``` Output: ``` 2 ``` #### Subtasks - **Subtask 1 (17 points)**: Max 100 inversions. - **Subtask 2 (18 points)**: $a_i \in \{1, 2\}$. - **Subtask 3 (25 points)**: String contains only 'L'. - **Subtask 4 (20 points)**: $n \le 10^5$. - **Subtask 5 (20 points)**: No additional constraints.
PTNK Team Selection Test [2025 - 2026]
Balanced subarray
Construct number
Orthogonal pairs
Bubble sort
Optimal subset
Water troughs
Topic
2026
Rating 800
Solution (1) Solution