HDU 3687 National Day Parade
来源:互联网 发布:串行12864接单片机 编辑:程序博客网 时间:2024/05/02 03:04
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
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<=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.
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.
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 1682 1011 1271 1052 900 0
Sample Output
41
Source
2010 Asia Hangzhou Regional Contest
Recommend
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;}
- HDU 3687 National Day Parade
- hdu 3687 National Day Parade
- HDU 3687 National Day Parade
- hdu 3687 National Day Parade
- HDU 3687 National Day Parade
- HDU 3687 National Day Parade (枚举)
- Hdu 3687 National Day Parade(暴力)
- HDU 3687 National Day Parade(暴力)
- HDU3687 National Day Parade
- hdu3687 National Day Parade(排成最后的n*n方阵最少走几步)
- National Day
- Happy National Day!
- National Day log 1
- National Day log 2
- This National Puppy Day
- Another National Day Holiday past...
- UVALive 4327 Parade(hdu 2490 Parade)
- HDU - 2490 Parade
- IO学霸
- 为图层上的N多怪物创建站立时的动画
- 21句经典语录
- MyEclipse开发JAX-RS架构WebServices收发JSON数据格式
- linux目录结构
- HDU 3687 National Day Parade
- AngularJS中service与factory的使用
- 黑马程序员---<<基础加强---1.5新特性(中)(注解(Annotation))>>
- 什么是Dos攻击?
- iOS 设置UILabel的背景图片
- SHTSC记 13.10.19
- myeclipse配置tomcat
- IOS7 隐藏状态栏 (电池栏)
- django如何使ForeignKey字段显示树状结构