QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 100

#1171. สับลำดับอาร์เรย์จำนวนเต็ม

Statistics

กำหนดอาร์เรย์จำนวนเต็ม $A$ ขนาด $N$ มาให้ นิยามการดำเนินการสับลำดับดังต่อไปนี้

  • เริ่มต้น สร้างอาร์เรย์จำนวนเต็มว่าง $B$
  • จากนั้น ขณะที่ $A$ ยังไม่ว่าง ให้นำสมาชิกซ้ายสุดหรือขวาสุดของ $A$ ออก แล้วนำค่านั้นไปต่อท้ายทางขวาของ $B$
  • หาก $A$ ว่าง ให้แทน $A$ ด้วย $B$ แล้วหยุด
problem_1171_89c88ed9b4827b8c5abe06dc4e63c1e5.png

หากดำเนินการสับลำดับตามรูปข้างต้น ค่าในอาร์เรย์ $A$ จะเปลี่ยนดังนี้:

$$ (34,19,5,36,4,25,12,9)\to(9,34,19,12,25,4,5,36). $$

ให้ $A_i$ เป็นสมาชิกที่ $i$ ของอาร์เรย์ $A$ หากเงื่อนไข “ถ้า $1 \le i<j \le N$ แล้ว $A_i \le A_j$” เป็นจริง จะกล่าวว่าอาร์เรย์ $A$ เพิ่มแบบทางเดียว

จงเขียนโปรแกรมที่รับอาร์เรย์จำนวนเต็ม $A$ ขนาด $N$ แล้วคำนวณจำนวนการสับลำดับน้อยที่สุดที่ต้องใช้เพื่อทำให้อาร์เรย์ $A$ เพิ่มแบบทางเดียว

ข้อมูลนำเข้า

บรรทัดแรกของข้อมูลนำเข้ามีจำนวนเต็มหนึ่งจำนวน $N$ ซึ่งเป็นจำนวนสมาชิกของอาร์เรย์ $A$ ($1 \le N \le 3\cdot 10^5$)

บรรทัดที่สองมีจำนวนเต็ม $N$ จำนวน $A_1,\ldots,A_N$ ซึ่งเป็นค่าเริ่มต้นของสมาชิกในอาร์เรย์ $A$ ($1 \le A_i \le 10^9$)

ข้อมูลส่งออก

พิมพ์จำนวนการสับลำดับน้อยที่สุดที่ต้องใช้เพื่อทำให้อาร์เรย์ $A$ เพิ่มแบบทางเดียว

ตัวอย่าง

ข้อมูลนำเข้า 1

3
2 2 5

ข้อมูลส่งออก 1

0

ข้อมูลนำเข้า 2

6
1 5 8 10 3 2

ข้อมูลส่งออก 2

1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.