hdu 3687 10 杭州 现场 H - National Day Parade 暴力水题

H - National Day Parade

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 3687

Appoint description: 
System Crawler  (2014-11-08)

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.

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

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;
int p[100][100];
int main()
{
    int n,m,x,y;
    while(scanf("%d %d",&n,&m)&&n&&m)
    {
        memset(p,0,sizeof(p));
        for(int i=0;i<n*n;i++)
        {
            scanf("%d %d",&x,&y);
            p[x][0]++;
            p[x][p[x][0]]=y;
        }
        for(int i=1;i<=n;i++)
            sort(p[i]+1,p[i]+n+1);
        int sum=0,minn=1000000000;
        for(int i=1;i<=m-n+1;i++)
        {
            sum=0;
            for(int j=1;j<=n;j++)
                for(int k=1;k<=n;k++)
                {
                    sum+=abs(i-1+k-p[j][k]);
                }
            minn=min(sum,minn);
        }
        printf("%d\n",minn);
    }
    return 0;
}

  

时间: 2025-01-01 09:29:35

hdu 3687 10 杭州 现场 H - National Day Parade 暴力水题的相关文章

hdu 3682 10 杭州 现场 C - To Be an Dream Architect 简单容斥

C - To Be an Dream Architect Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3682 Appoint description:  System Crawler  (2014-11-09) Description The “dream architect” is the key role in a team o

hdu 3685 10 杭州 现场 F - Rotational Painting 重心

F - Rotational Painting Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3685 Appoint description:  System Crawler  (2014-11-09) Description Josh Lyman is a gifted painter. One of his great works

hdu 3697 10 福州 现场 H - Selecting courses

Description A new Semester is coming and students are troubling for selecting courses. Students select their course on the web course system. There are n courses, the ith course is available during the time interval (A i,B i). That means, if you want

hdu 4771 13 杭州 现场 B - Stealing Harry Potter&#39;s Precious 暴力bfs

Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know, uncle Ve

hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机

F - Computer Virus on Planet Pandora Time Limit:2000MS     Memory Limit:128000KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3695 Appoint description:  System Crawler  (2014-11-05) Description Aliens on planet Pandora also write comp

hdu 3699 10 福州 现场 J - A hard Aoshu Problem

Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. Nowadays, Aoshu is getting more and more difficult. Here is a classic Aoshu problem: ABBDE __ ABCCC = BDBDE In the equation above, a letter stands for

hdu 3694 10 福州 现场 E - Fermat Point in Quadrangle

In geometry the Fermat point of a triangle, also called Torricelli point, is a point such that the total distance from the three vertices of the triangle to the point is the minimum. It is so named because this problem is first raised by Fermat in a

hdu 3696 10 福州 现场 G - Farm Game

Description “Farm Game” is one of the most popular games in online community. In the community each player has a virtual farm. The farmer can decide to plant some kinds of crops like wheat or paddy, and buy the corresponding crop seeds. After they gr

hdu 4770 13 杭州 现场 A - Lights Against Dudely 暴力 bfs

Description Harry: "But Hagrid. How am I going to pay for all of this? I haven't any money." Hagrid: "Well there's your money, Harry! Gringotts, the wizard bank! Ain't no safer place. Not one. Except perhaps Hogwarts." ― Rubeus Hagrid