#FT0000234. Số vòng sắp xếp nổi bọt I (Bubble Sort Rounds I)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Số vòng sắp xếp nổi bọt I (Bubble Sort Rounds I)
Bubble Sort Rounds I
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
An array of integers is sorted by bubble sort. In one round, the algorithm scans adjacent pairs from left to right, examining positions in order. If the element currently at position is greater than the element at position , the two elements are swapped immediately. Equal elements are not swapped. Each later comparison uses the array as modified by preceding swaps in the same round.
Determine the minimum number of rounds needed to make the array nondecreasing. The answer is if the array is already sorted. Do not count an extra no-swap verification round after the array is sorted.
Input
- The first line contains the integer , the array length.
- The second line contains exactly integers from left to right.
Output
Print one integer: the required number of bubble sort rounds.
Subtasks
- Subtask 1 (20 points): ; for every .
- Subtask 2 (30 points): ; for every .
- Subtask 3 (50 points): ; for every .
Examples
Input
5
3 2 4 1 4
Output
3
Explanation
Initially the array is . Each round compares adjacent pairs from left to right, using the result of each swap immediately.
- After round : . The pairs and then are swapped.
- After round : . The value moves one more position to the left.
- After round : . The array is now nondecreasing.
The array was still unsorted after round because its first two elements were . Hence the answer is .