National Day Parade HDU - 3687

National Day Parade

 HDU - 3687 

There are n×n students preparing for the National Day parade on the playground. The playground can be considered as a n×m grid. The coordinate of the west north corner is (1,1) , and the coordinate of the east south corner is (n,m). 


When training, every students must stand on a line intersection and all students must form a n×n square. The figure above shows a 3×8 playground with 9 students training on it. The thick black dots stand for the students. You can see that 9 students form a 3×3 square. 

After training, the students will get a time to relax and move away as they like. To make it easy for their masters to control the training, the students are only allowed to move in the east-west direction. When the next training begins, the master would gather them to form a n×n square again, and the position of the square doesn’t matter. Of course, no student is allowed to stand outside the playground. 

You are given the coordinates of each student when they are having a rest. Your task is to figure out the minimum sum of distance that all students should move to form a n×n square. 

Input

There are at most 100 test cases. 
For each test case: 
The first line of one test case contain two integers n,m. (n<=56,m<=200) 
Then there are n×n lines. Each line contains two integers, 1<=Xi<=n,1<= Y i<=m indicating that the coordinate of the i th student is (X i , Y i ). It is possible for more than one student to stand at the same grid point. 

The input is ended with 0 0. 

Output

You should output one line for each test case. The line contains one integer indicating the minimum sum of distance that all students should move to form a n×n square.

Sample Input

2 168
2 101
1 127
1 105
2 90
0 0

Sample Output

41

 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
using namespace std;

vector<int> a[60];

int main() {
	
	int n, m;
	while (~scanf ("%d %d", &n, &m) && (n+m)) {
		
		for (int i = 1; i <= n; ++i) a[i].clear();
		int x, y, k = n*n;
		for (int i = 1; i <= k; ++i) {
			scanf ("%d %d", &x, &y);
			a[x].push_back(y);
		}
		for (int i = 1; i <= n; ++i) sort(a[i].begin(), a[i].end());
		int mi = 0x3f3f3f3f, ma=-mi;
		for (int i = 1; i <= n; ++i) { //确定左上角右上角
			ma = max(a[i][n-1], ma);
			mi = min(a[i][0], mi);
		}
		//printf ("debug: %d %d\n", ma, mi);
		int ans = 0x3f3f3f3f;
		for (int i = mi; i <= ma; ++i) { //暴力 
			int sum = 0;
			for (int j = 0; j < n; ++j) {
				for (int g = 1; g <= n; ++g) {
					sum += abs(a[g][j] - (i+j));
				}
			}
			//printf ("sun = %d\n", sum);
			ans = min(ans, sum);
		}
		printf ("%d\n", ans);
		
	}
	
	
	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值