文件名称:Soldiers
- 所属分类:
- 其他小程序
- 资源属性:
- [Windows] [Visual C] [源码]
- 上传时间:
- 2012-12-11
- 文件大小:
- 1.13mb
- 下载次数:
- 0次
- 提 供 者:
- 孟**
- 相关连接:
- 无
- 下载说明:
- 别用迅雷下载,失败请重下,重下不扣分!
介绍说明--下载内容均来自于网络,请自行研究使用
【问题描述】
在一个划分成网格的操场上,n个士兵散乱地站在网格点上。网格点由整数坐标(x,y)表示。士兵们可以沿网格边上、下、左、右移动一步,但在同一时刻任一网格点上只能有一名士兵。按照军官的命令,士兵们要整齐地列成一个水平队列,即排列成(x,y),(x+1,y),…,(x+n-1,y)。如何选择x 和y的值才能使士兵们以最少的总移动步数排成一列。
【算法设计】
计算使所有士兵排成一行需要的最少移动步数。
【数据输入】
由文件input.txt 提供输入数据。文件的第1 行是士兵数n,1≤n≤10000。接下来n 行是士兵的初始位置,每行2 个整数x和y,-10000≤x,y≤10000。
【结果输出】
将计算结果输出到文件output.txt 中。文件的第1行中的数是士兵排成一行需要的最少移动步数。
输入文件示例 输出文件示例
input.txt output.txt
5 8-SOLDIERS
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Descr iption
N soldiers of the land Gridland are randomly scattered around the country. A position in Gridland is given by a pair (x,y) of integer coordinates. Soldiers can move- in one move, one soldier can go one unit up, down, left or right (hence, he can change either his x or his y coordinate by 1 or-1). The soldiers want to get into a horizontal line next to each other (so that their final positions are (x,y), (x+1,y), ..., (x+N-1,y), for some x and y). Integers x and y, as well as the final order of soldiers along the horizontal line is arbitrary. The goal is to minimise the total number of moves of all the soldiers that takes them into such configuration. Two or more soldiers must never occupy the same position at the same time.
Input
The first line of the input contains the integer N, 1 <= N <= 10000, the number of soldiers. The following N lines of the input conta
(系统自动生成,下载前可以参看下载内容)
下载文件列表
Soldiers
........\Debug
........\.....\Soldiers.exe
........\.....\Soldiers.ilk
........\.....\Soldiers.obj
........\.....\Soldiers.pch
........\.....\Soldiers.pdb
........\.....\vc60.idb
........\.....\vc60.pdb
........\Soldiers.cpp
........\Soldiers.dsp
........\Soldiers.dsw
........\Soldiers.ncb
........\Soldiers.opt
........\Soldiers.plg
........\Soldiers思路.txt
........\题目.txt