POJ - 1723
Time Limit:1000MS | Memory Limit:10000KB | 64bit IO Format:%I64d & %I64u |
Description
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.
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 contain initial positions of the soldiers : for each i, 1 <= i <= N, the (i+1) st line of the input file contains a pair of integers x[i] and y[i] separated by a single blank character, representing the coordinates of the ith soldier, -10000 <= x[i],y[i] <= 10000.
The following N lines of the input contain initial positions of the soldiers : for each i, 1 <= i <= N, the (i+1) st line of the input file contains a pair of integers x[i] and y[i] separated by a single blank character, representing the coordinates of the ith soldier, -10000 <= x[i],y[i] <= 10000.
Output
The first and the only line of the output should contain the minimum total number of moves that takes the soldiers into a horizontal line next to each other.
Sample Input
5 1 2 2 2 1 3 3 -2 3 3
Sample Output
8
Source
解析
每个士兵的x,y互不影响,可以将x,y离散。
这个题有个结论:若有一个x可以使sigma(abs(a[i]-x))最小,那么x=数组a的中位数。那么题上的第一个要求:站成水平的一行,就很好满足了——找到y的中位数。
第二个要求:排成一行。如果最中间的人集合地点x值m,那么他左边的人的集合地点是m-1,右边的人的集合地点是m+1……为了方便计算,我把左边的人横坐标值+1,右边的人横坐标-1,两人等价集合到m点。以此类推,我处理完整个数组,问题又变成把所以的人集合到一点。所以只要把新数组排个序,求中位数即可。
如此一来集合地点就找到了,计算代价输出即可。
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
int x[10100],y[10100],N;
int main()
{
scanf("%d",&N);
for(int i=1;i<=N;i++)
scanf("%d%d",&x[i],&y[i]);
sort(x+1,x+1+N); sort(y+1,y+1+N);
int midy=y[(N>>1)+1];
for(int i=1;i<=N;i++)
x[i]+=((N-1)>>1)+1-i;
sort(x+1,x+1+N);
int midx=x[(N>>1)+1],ans=0;
for(int i=1;i<=N;i++) ans+=abs(midx-x[i])+abs(midy-y[i]);
printf("%d",ans);
return 0;
}