#FT0000076. Sắp xếp chỗ ngồi – Bản khó (Seating Arrangements (Hard Version))
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sắp xếp chỗ ngồi – Bản khó (Seating Arrangements (Hard Version))
Seating Arrangements (Hard Version)
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A cinema has rows, each with seats. Seats are numbered left to right and then row by row: row has seat numbers through . Smaller seat numbers provide a better view.
Exactly spectators arrive in order . Spectator has eyesight level and must receive one unique seat. An assignment is valid only if implies that the seat number of spectator is smaller than that of spectator . Spectators with equal eyesight may be arranged arbitrarily among the corresponding seats.
When entering, each spectator walks along their assigned row from the left to their seat. Their inconvenience equals the number of already-occupied seats strictly to the left of theirs in the same row; empty seats do not count.
Minimize the sum of inconveniences over all valid seat assignments.
Input
The first line contains integers , the number of rows and seats per row.
The second line contains exactly integers , the eyesight levels in arrival order.
Output
Print one integer: the minimum total inconvenience across all spectators.
Subtasks
-
Subtask 1 (20 points): ; for all ; .
-
Subtask 2 (30 points): ; for all ; .
-
Subtask 3 (50 points): ; for all ; .
Examples
Input
2 2
1 1 2 1
Output
0
Explanation
There are two rows with two seats each, and eyesight levels . The three spectators with level must occupy seats , while spectator with level receives seat .
Assign seats to spectators respectively.
- Spectator finds the first row empty: inconvenience .
- Spectator goes to seat , with no seat to its left: .
- Spectator finds the second row empty upon taking seat : .
- Spectator goes to seat , the leftmost seat in row two: .
The total is , which is minimal because inconvenience is nonnegative. Print 0.