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. Python量化教程 常用函数

    # -*- coding: utf-8 -*- # @Author: fangbei # @Date: 2017-08-26 # @Original: price_str = '30.14, 29.5 ...

  2. P2750 贰五语言Two Five USACO5.5 记忆化搜索

    正解:记搜+逼近 解题报告: 神仙题预警,,, 我真滴觉得还是挺难的了,,, 大概说下思路趴QAQ 首先我们要知道逼近法是什么! 逼近法,有点像二分的思路,以这题为例举个eg 假如它给了个数字k.我们 ...

  3. SDWebimage清空缓存

    清空缓存 - (void)clearTmpPics{ [[SDImageCache sharedImageCache] clearDisk]; [[SDImageCache sharedImageCa ...

  4. Randcher 2.0部署K8s集群(一)

    环境准备 1.系统版本 CentOS7.5 + docker ee 2.配置阿里云yum源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirro ...

  5. HDU1575:Tr A(矩阵快速幂模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1575   #include <iostream> #include <string.h> ...

  6. samba安装测试

    1.检查是否系统有自带的samba安装包 2.关闭防火墙 Iptables -F Systemctl disable firewalld Systemctl stop firewalld System ...

  7. html 音频

    <!DOCTYPE html><meta charset="utf-8"><video src="movie.webm" cont ...

  8. supervisor安装及其配置

    一.supervisor概述 supervisor是一个c/s系统,被用来在类Unix系统中监控进程状态.supervisor使用python开发. 服务端进程为supervisord,主要负责启动自 ...

  9. tslib移植中环境变量编辑

    (1)将/usr/local/tslib下的所有文件复制到移植系统文件中/usr/local(2)编辑移植系统中/etc/profile添加触摸屏支持内容:在/etc/profile文件中设置tsli ...

  10. bzoj1611 / P2895 [USACO08FEB]流星雨Meteor Shower

    P2895 [USACO08FEB]流星雨Meteor Shower 给每个点标记一下能够走的最迟时间,蓝后bfs处理一下 #include<iostream> #include<c ...