H - National Day Parade

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

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
题意:
有n*n学生本来站成n*n方阵((1,1)->(n,n)),现在左右移动了,告诉你之前的坐标和现在的坐标,求他们移动的最小距离
思路:
1. 此题只有左右移动,明显一行之内就只需要处理本行就行了
2. 数据范围不会超int
3. 原本的位置就是在最左边,现在相当于全是向右偏了,所以全部加起来-(1*n)*n/2即可
 
这是某io07的代码:
 
#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;
}

  

 

hdu 3687 10 杭州 现场 H - National Day Parade 水题 难度:0的更多相关文章

  1. hdu 3699 10 福州 现场 J - A hard Aoshu Problem 暴力 难度:0

    Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...

  2. hdu 3685 10 杭州 现场 F - Rotational Painting 重心 计算几何 难度:1

    F - Rotational Painting Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  3. hdu 3682 10 杭州 现场 C - To Be an Dream Architect 简单容斥 难度:1

    C - To Be an Dream Architect Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  4. hdu 3682 10 杭州 现场 C To Be an Dream Architect 容斥 难度:0

    C - To Be an Dream Architect Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  5. hdu 3697 10 福州 现场 H - Selecting courses 贪心 难度:0

    Description     A new Semester is coming and students are troubling for selecting courses. Students ...

  6. hdu 2553:N皇后问题(DFS遍历,水题)

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. HDU——2066一个人的旅行(优先队列SPFA水题)

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  8. hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1

    F - Computer Virus on Planet Pandora Time Limit:2000MS     Memory Limit:128000KB     64bit IO Format ...

  9. hdu 3696 10 福州 现场 G - Farm Game DP+拓扑排序 or spfa+超级源 难度:0

    Description “Farm Game” is one of the most popular games in online community. In the community each ...

随机推荐

  1. Intellij idea的Dependencies波浪线

    昨天在家做项目不知道搞了什么出现了大量波浪线.搞了大半天解决了下面的问题. 1.pom.xml出现波浪线.看右边 maven project-->Profiles 勾选dev 2.上面已勾选还有 ...

  2. stark - filter、pop、总结

    一.filter 效果图 知识点 1.配置得显示Filter,不配置就不显示了 list_filter = ['title','publish', 'authors'] 2.前端显示 后端返回 字典 ...

  3. Unity3d GUI适应分辨率

    float sourceWidth = 1024f; float sourceHeight = 768f; float m_fScaleWidth; float m_fScaleHeight; voi ...

  4. python selenium 安装与 chromedriver安装

    安装 pip install selenium 安装完成之后运行脚本,如果没报错那ok.但是很不幸运,我报错啦.(本人使用ubuntu16.04,python2,or python3) 贴出我的报错: ...

  5. 在GUI程序中使用控制台的两种方法

    win32程序启用控制台(控制台文件名:conout$,conin$,conerr$) //添加控制台,加入在程序构造函数中 AllocConsole(); freopen("conin$& ...

  6. 2017年浙江中医药大学程序设计竞赛 Solution

    训练地址 A: 树剖板子题 求最小值的时候要注意值是不是有负数,如果有,初值要置为$-INF$ #include <bits/stdc++.h> using namespace std; ...

  7. CSS实现超出DIV宽度文字自动隐藏并显示省略号

    当文字超出DIV宽度时,超出的文字部分省略,并用显示省略号代替,css代码如下: div.ellipsis { padding-left: 5px; text-align: left; text-ov ...

  8. Win7系统(台式机)设置系统的窗口背景色(豆沙绿色)

    Win7系统(台式机)设置系统的窗口背景色(豆沙绿色) 1,桌面->右键->个性化->窗口颜色->高级外观设置->项目选择(窗口).颜色1(L)选择(其它)将色调改为:8 ...

  9. ubuntu18.04系统安装+基本环境配置【原创】

    平台信息:PC:ubuntu18.04.i5.七彩虹GTX1060显卡.固态硬盘.机械硬盘 作者:庄泽彬(欢迎转载,请注明作者) 说明:在原本的电脑买一个独立显卡,装上去之后,出了各种问题,虽然后面勉 ...

  10. 框架-springmvc源码分析(一)

    框架-springmvc源码分析(一) 参考: http://www.cnblogs.com/heavenyes/p/3905844.html#a1 https://www.cnblogs.com/B ...