National Day Parade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1383 Accepted Submission(s): 596
Problem Description
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.
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.
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
Source
Recommend
lcy&zhengfeng
本题给定n*n个点的坐标,然后要在指定的n*m的方块内移动最少的次数使n*n个点构成一个连成一个正方形的小方块。
本题数据很小,可以直接模拟。枚举连成正方形小方块的左边界,然后枚举所有点的距离和,保证每行n个点的相对顺序不变。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN=60*60;
const int INF=200*MAXN;
int n,m;
struct point
{
int x,y;
}Point[MAXN];
bool cmp(point a,point b)
{
if(a.x!=b.x)return a.x<b.x;
return a.y<b.y;
}
void Solve()
{
int i,j,t,dis=INF,tmp;
Point[0].x=-1;
for(i=1;i<=m;i++)
{
tmp=0;
for(j=1;j<=n*n;j++)
{
if(Point[j].x!=Point[j-1].x)
{
t=i;
}
tmp+=abs(Point[j].y-t);
t++;
}
if(dis>tmp)dis=tmp;
}
printf("%d\n",dis);
}
int main()
{
int i;
while(~scanf("%d%d",&n,&m))
{
if(0==n&&0==m)break;
for(i=1;i<=n*n;i++)
scanf("%d%d",&Point[i].x,&Point[i].y);
sort(Point+1,Point+1+n*n,cmp);
Solve();
}
return 0;
}