#FT0000036. Sắp xếp các lá bài (Cards Sorting)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sắp xếp các lá bài (Cards Sorting)
Cards Sorting
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Vasily has a stack of cards, initially arranged from top to bottom in the given order. Every card has an integer value; equal values are allowed.
He repeatedly looks at the top card; every such look counts as one inspection. If its value is the minimum among all cards still in the stack, he permanently removes it. Otherwise, he puts it at the bottom, preserving the order of the other cards.
The process ends when the stack is empty. Find the total number of top-card inspections, including cards inspected and moved to the bottom instead of removed. This number may exceed .
Input
- Line 1: integer , the number of cards.
- Line 2: integers , ordered from top to bottom.
Output
Print one integer: the total number of times the top card is inspected before the stack becomes empty.
Subtasks
Subtask 1 (20 points):
- ; for every .
Subtask 2 (30 points):
- ; for every .
Subtask 3 (50 points):
- ; for every .
Examples
Examples 1
Input
4
6 3 1 2
Output
7
Explanation
Starting with , inspect , , then ; only is removed (3 inspections). The remaining order is . Inspect and remove (4th), inspect and move it, inspect and remove (5th and 6th), and finally remove on the 7th inspection.
Examples 2
Input
7
3 3 3 3 3 3 3
Output
7
Explanation
Every card has the current minimum value , so each inspection immediately removes one card. Exactly inspections are needed.
No card has a value below , so the first card examined is always eligible for removal. The remaining six, then five, and so on, still all have value . Exactly one examination removes each card, giving examinations in total.