MCARDS - Card Sorting

 

Dave’s little son Maverick likes to play card games, but being only four years old, he always lose when playing with his older friends. Also, arranging cards in his hand is quite a problem to him.

When Maverick gets his cards, he has to arrange them in groups so that all the cards in a group are of the same color. Next, he has to sort the cards in each group by their value – card with lowest value should be the leftmost in its group. Of course, he has to hold all the cards in his hand all the time.

He has to arrange his cards as quickly as possible, i.e. making as few moves as possible. A move consists of changing a position of one of his cards.

Write a program that will calculate the lowest number of moves needed to arrange cards.

 

Input

 

The first line of input file contains two integers C, number of colors (1 ≤ C ≤ 4), and N, number of cards of the same color (1 ≤ N ≤ 100), separated by a space character.

Each of the next C*N lines contains a pair of two integers X and Y, 1 ≤ X ≤ C, 1 ≤ Y ≤ N, separated by a space character.

Numbers in each of those lines determine a color (X) and a value (Y) of a card dealt to little Maverick. The order of lines corresponds to the order the cards were dealt to little Maverick. No two lines describe the same card.

 

Output

 

The first and only line of output file should contain the lowest number of moves needed to arrange the cards as described above.

 

Sample

CARDS.IN

2 2
2 1
1 2
1 1
2 2

CARDS.OUT

2
 
CARDS.IN

4 1
2 1
3 1
1 1
4 1

CARDS.OUT

0
 
CARDS.IN

3 2
3 2
2 2
1 1
3 1
2 1
1 2

CARDS.OUT
2


Được gửi lên bởi:psetter
Ngày:2009-03-13
Thời gian chạy:1s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO NODEJS PERL6 PYPY RUST SED VB.NET
Nguồn bài:COI 01

hide comments
2021-05-27 18:01:43
Tham khảo: https://vnspoj.github.io/problems/MCARDS
2020-02-16 14:41:34
mình ngu quá :(((
2018-09-01 08:29:01
nhác cài LIS nên dùng LCS vẫn AC 0,05s
2016-11-07 03:35:41
bài này tiếng Việt khó hiểu
2015-08-19 15:55:17
THAM KHẢO TẠI https://traitaodo.wordpress.com/2015/08/19/sap-xep-cac-quan-bai-mcards/

Last edit: 2015-09-13 11:11:45
2015-06-23 11:56:00 Bee
1 đấm AC
2015-02-05 18:38:42 Con Bò Huyền Thoại
http://dangminhtien.name.vn/blog/2015/02/05/mcards-spoj-card-sorting/
2014-04-05 16:49:39 Thanga2pbc
lis>>ac :v

Last edit: 2014-04-05 16:53:20
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.