HDU 3687 National Day Parade
2013-10-19 14:35
190 查看
National Day Parade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1442 Accepted Submission(s): 634
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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<=Xi<=n,1<= Yi<=m indicating that the coordinate of the ith
student is (Xi , Yi ). It is possible for more than one student to stand at the same grid point.
The input is ended with 0 0.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2 168 2 101 1 127 1 105 2 90 0 0
[align=left]Sample Output[/align]
41
[align=left]Source[/align]
2010 Asia Hangzhou Regional Contest
[align=left]Recommend[/align]
lcy&zhengfeng
说实话,我被这题坑了。枚举可以过的题,可是我在一旁找规律,YY算法,结果错了2次。
找出的规律:(这个规律在枚举中不需要使用的)
单独考虑一排时:
n个人集合,确定最左边的人的位置范围在posleft~posright-n+1中时,结果是一样的并且最小。(n为偶数时)
最中间的那个人不动,其他人向他靠拢就是最小值。(n为奇数,可以推出最左边人的位置:posmid-n/2)
但是由一维推往二维时,我找不到正确的规律了。
求各位大神解答!!!
#include<iostream> #include<stdio.h> #include<vector> #include<queue> #include<stack> #include<algorithm> #include<math.h> #include<string> #include<map> #include<set> #include<string.h> using namespace std; vector<int> num[57]; int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ if(!n&&!m){ break; } int i,j,k,a,b,ans=2000000000,f,ma=0; for(i=0;i<=n;i++){ num[i].clear(); } for(i=0;i<n*n;i++){ scanf("%d%d",&a,&b); num[a].push_back(b); } for(i=1;i<=n;i++){ sort(num[i].begin(),num[i].end()); if(num[i][n-1]>ma) ma=num[i][n-1]; } for(int x=1;x<=ma-n+1;x++){ f=0; k=x; for(i=1;i<=n;i++){ for(j=0;j<n;j++){ f+=abs(k+j-num[i][j]); } } if(f<ans) ans=f; } printf("%d\n",ans); } return 0; }
相关文章推荐
- Ubuntu 12.10 安装 JDK 7u15官方版
- The Elements of Style
- 构建httpd网站服务器(二) -- httpd服务的访问控制和基于域名、IP、端口的虚拟主机
- 03链表的创建、插入、删除、遍历、排序
- 小菜的系统框架界面设计-小菜用户权限管理软件开源大放送
- 求最大连续子串
- 分享一个jdk源码链接
- oracle 直接修改timestamp记录
- int main(int argc,char *argv[])
- 02数组操作
- hdu 1717 小数化分数2
- Linux 开发中的常用命令 ( Cygwin 环境也同样适用)
- Linux 开发中的常用命令 ( Cygwin 环境也同样适用)
- django如何使ForeignKey字段显示树状结构
- 正则表达式——入门
- 为图层上的N多怪物创建站立时的动画
- IOS7 隐藏状态栏 (电池栏)
- C#实现大数字的运算
- Codeforces Round #207 (Div. 2) A. Group of Students
- epoll()学习