#FT0000043. Làm rỗng mảng (Make Array Empty)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Làm rỗng mảng (Make Array Empty)
Make Array Empty
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of pairwise distinct integers. Repeatedly perform one of the following operations until the array becomes empty:
- If the current first element is the minimum of the entire remaining array, remove it.
- Otherwise, move the current first element to the end, preserving the relative order of the other elements.
Each action counts as one operation, including moving an element without removing it. After every operation, apply the rule to the updated array. Return the total number of operations. Because the input values are distinct, the current minimum is unique.
Input
- Line 1: integer , the array length.
- Line 2: exactly integers in initial order.
Output
Print one integer: the total number of operations required to empty the array.
Subtasks
Subtask 1 (20 points):
- ; for all ; for .
Subtask 2 (30 points):
- ; for all ; for .
Subtask 3 (50 points):
- ; for all ; for .
Examples
Examples 1
Input
3
3 4 -1
Output
5
Explanation
Start with . Move to the end (step 1), then move to the end (step 2), reaching . Remove (step 3), then (step 4), and finally (step 5).
Examples 2
Input
4
1 2 4 3
Output
5
Explanation
Remove and immediately (2 steps), leaving . Rotate to the end (step 3), remove (step 4), and remove (step 5).
The complete sequence of states is $[1,2,4,3] \to [2,4,3] \to [4,3] \to [3,4] \to [4] \to []$. The first two, fourth, and last transitions remove an element; the third moves to the back. This makes four removals and one move, for operations.