301658: CF316C1. Tidying Up
Memory Limit:256 MB
Time Limit:4 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Tidying Up
题意翻译
## 题意简述 给定一个$n*m$的矩阵(保证$n*m$为偶数),矩阵中每个数字的取值范围为$[1,\dfrac{n*m}{2}]$,保证每个数字恰好出现两次,你可以选择交换任意次矩阵中的任意两个相邻数,要求交换后的矩阵中任意两个相同的数字位置相邻,并且要求改变后不再原来位置的数尽可能少,请求出这个最小值。 ## 数据范围 对于 $30\%$ 的数据,满足 $2 \leq n,m \leq 8$ 。(提交[C1](https://www.luogu.com.cn/problem/CF316C1)) 对于 $100\%$ 的数据,满足 $2 \leq n,m \leq 80$ 。(提交[C1](https://www.luogu.com.cn/problem/CF316C1)和[C2](https://www.luogu.com.cn/problem/CF316C2))题目描述
Smart Beaver is careful about his appearance and pays special attention to shoes so he has a huge number of pairs of shoes from the most famous brands of the forest. He's trying to handle his shoes carefully so that each pair stood side by side. But by the end of the week because of his very active lifestyle in his dressing room becomes a mess. Smart Beaver from ABBYY is not only the brightest beaver in the area, but he also is the most domestically oriented. For example, on Mondays the Smart Beaver cleans everything in his home. It's Monday morning. Smart Beaver does not want to spend the whole day cleaning, besides, there is much in to do and it’s the gym day, so he wants to clean up as soon as possible. Now the floors are washed, the dust is wiped off — it’s time to clean up in the dressing room. But as soon as the Smart Beaver entered the dressing room, all plans for the day were suddenly destroyed: chaos reigned there and it seemed impossible to handle, even in a week. Give our hero some hope: tell him what is the minimum number of shoes need to change the position to make the dressing room neat. The dressing room is rectangular and is divided into $ n×m $ equal squares, each square contains exactly one shoe. Each pair of shoes has a unique number that is integer from $ 1 $ to ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF316C1/62bb55bec1337f59eb380936fd7dc7362196cf87.png), more formally, a square with coordinates $ (i,j) $ contains an integer number of the pair which is lying on it. The Smart Beaver believes that the dressing room is neat only when each pair of sneakers lies together. We assume that the pair of sneakers in squares $ (i_{1},j_{1}) $ and $ (i_{2},j_{2}) $ lies together if $ |i_{1}-i_{2}|+|j_{1}-j_{2}|=1 $ .输入输出格式
输入格式
The first line contains two space-separated integers $ n $ and $ m $ . They correspond to the dressing room size. Next $ n $ lines contain $ m $ space-separated integers each. Those numbers describe the dressing room. Each number corresponds to a snicker. It is guaranteed that: - $ n·m $ is even. - All numbers, corresponding to the numbers of pairs of shoes in the dressing room, will lie between $ 1 $ and ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF316C1/62bb55bec1337f59eb380936fd7dc7362196cf87.png). - Each number from $ 1 $ to ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF316C1/62bb55bec1337f59eb380936fd7dc7362196cf87.png) will occur exactly twice. The input limits for scoring 30 points are (subproblem C1): - $ 2<=n,m<=8 $ . The input limits for scoring 100 points are (subproblems C1+C2): - $ 2<=n,m<=80 $ .
输出格式
Print exactly one integer — the minimum number of the sneakers that need to change their location.
输入输出样例
输入样例 #1
2 3
1 1 2
2 3 3
输出样例 #1
2
输入样例 #2
3 4
1 3 2 6
2 1 5 6
4 4 5 3
输出样例 #2
4