MCARDS - Card Sorting

no tags 

 

Mav có n×c quân bài; mỗi quân bài 1 màu; và mỗi màu có n quân bài. Mav muốn sắp xếp các quân bài cùng màu nằm cạnh nhau và chúng theo thứ tự tăng dần về giá trị.

Input

Dòng đầu là 2 số C (số màu), (1 ≤ C ≤ 4), và N, số quân bài cùng màu mỗi loại, (1 ≤ N ≤ 100).

N×C dòng tiếp theo mỗi dòng là 2 số nguyên X, Y; 1 ≤ X ≤ C, 1 ≤ Y ≤ N, là màu của quân bài và giá trị quân bài đó.

Thứ tự quân bài ban đầu (từ trái sang phải) chính là thứ tự dữ liệu input.

Output

Số lần chuyển bài ít nhất để thu được dãy bài được sắp theo yêu cầu trên.

Sample

Input:
2 2
2 1
1 2
1 1
2 2

Output:
2
Input:
4 1
2 1
3 1
1 1
4 1

Output:
0
Input:
3 2
3 2
2 2
1 1
3 1
2 1
1 2

Output:
2


hide comments
anubhav448: 2025-04-10 12:31:30

Min no. of moves --> means find which cards are in correct place or vice-versa. For that loop through all permutations of color groups and for each permutation create a custom ordering since once we know order of colors and their values, we can define the exact order for each card to appear in and then use LIS do determine which card is at wrong position and find min of all permutations to get answer. Not easy to come up with.
Concept - custom ordering with LIS.

chalapathi_444: 2021-07-02 06:16:24

feeling difficult


Added by:psetter
Date:2009-03-13
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:COI 01