hdu3687 National Day Parade(排成最后的n*n方阵最少走几步)

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<=X i<=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
读题陷阱啊!!!看红色的字:一开始他们是排好n*n方阵的,然后他们解散的时候每个人只会往东西方向走!!那么再要归队的时候肯定每行有且刚好n个人!!!

是不是发现问题很简单了╮(╯▽╰)╭

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1000000007
#define ll long long
using namespace std;
int main(){
    int n,m,a,b;
    while(scanf("%d%d",&n,&m)&&(n+m)){
        vector<int> x[58];
        for(int i=0;i<n*n;++i){
            cin>>a>>b;
            x[a].push_back(b);
        }
        for(int i=1;i<=n;++i)
            sort(x[i].begin(),x[i].end()); //同一行上的先排序(即它们的最后顺序)
        int s=(n-1)*n/2,ans=100000000;
        for(int i=1;i<=m-n+1;++i){  //枚举第一列的位置
            int ss=0;
            for(int j=1;j<=n;++j){
                for(int k=0;k<n;++k){
                    ss+=abs(x[j][k]-(i+k)); //每个人与最后位置的距离
                }
            }
            if(ss<ans)
                ans=ss;
        }
        cout<<ans<<endl;
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值