ACM训练合集---POJ - 1723 - SOLDIERS

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 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

题目大意

N个士兵散落在直角坐标系上,每个士兵对应一个坐标,他们只能上下左右的移动。现在题目要求,让士兵们排成一行,即y相等,x不可重复;问所有士兵总共最少移动多少步就可以排成一行。

解题思路:

  • 统计在坐标轴移动的步数可以将其分解成 x方向的步数 + y方向的步数
  • 假设n个士兵们纵坐标为 y 0 y 1 y 2 . . . y n y_0 y_1 y_2 ... y_n y0y1y2...yn ,队伍已经排好且横坐标为Y,则计算所有点在y方向最少步数 等价于 求 ∣ y 0 − Y ∣ + ∣ y 1 − Y ∣ + ∣ y 2 − Y ∣ . . . + ∣ y n − Y ∣ |y_0-Y| + |y_1-Y| +|y_2-Y| ...+|y_n-Y| y0Y+y1Y+y2Y...+ynY 的最小值,即Y要取纵坐标的中位数。(后面有证明为什么到中位数的距离最短)
  • 假设n个士兵们横坐标为 x 0 x 1 x 2 . . . x n x_0 x_1 x_2 ... x_n x0x1x2...xn ,队伍已经排好且开头的士兵的横坐标为X,则求x方向的最少步数等价于 ∣ x 0 − ( X + 0 ) ∣ + ∣ x 1 − ( X + 1 ) ∣ + ∣ x 2 − ( X + 2 ) ∣ . . . + ∣ x n − ( X + n ) ∣ |x_0-(X+0)| + |x_1-(X+1)| +|x_2-(X+2)| ...+|x_n-(X+n)| x0(X+0)+x1(X+1)+x2(X+2)...+xn(X+n) 的最小值为什么X要加0 1 2... 因为每个x坐标只能有一个士兵,所以需要士兵们需要排队。)
    • 可以将上述的 ∣ x 0 − ( X + 0 ) ∣ + ∣ x 1 − ( X + 1 ) ∣ + ∣ X 2 − ( X + 2 ) ∣ . . . + ∣ x n − ( X + n ) ∣ |x_0-(X+0)| + |x_1-(X+1)| +|X_2-(X+2)| ...+|x_n-(X+n)| x0(X+0)+x1(X+1)+X2(X+2)...+xn(X+n) 化成 ∣ ( x 0 − 0 ) − X ∣ + ∣ ( x 1 − 1 ) − X ∣ + ∣ ( x 2 − 2 ) − X ∣ + . . . + ∣ ( x n − n ) − X ∣ |(x_0-0) - X|+|(x_1-1) - X|+|(x_2-2) - X| +...+|(x_n-n) - X| (x00)X+(x11)X+(x22)X+...+(xnn)X ,要得到转化后式子的最小值,则X应该为(x_0-0)(x_1-1)(x_n-n)序列的中位数
  • 最后将x方向、y方向的最少步数相加即可。

数学证明:
|2-x|+|9-x|+|4-x|的最小值?
当x取中位数,即这里的4时,有最小值7.

不理解可以看下图:(把绝对值理解成距离)
在这里插入图片描述


代码如下:

#include<iostream>
#include<algorithm>

using namespace std;

bool bmpX(pair<int, int> a, pair<int, int> b){
	return a.first < b.first;
}

bool bmpY(pair<int, int> a, pair<int, int> b){
	return a.second < b.second;
}

int main(){
	int n, midX, midY;
	long long min = 0;
	int i, j;
	pair<int, int> p[10000];
	cin >> n;
	for (i = 0; i < n; i++){
		cin >> p[i].first >> p[i].second;
	}
	// 对x坐标进行排序
	sort(p, p + n, bmpX);
	// 对每个x减去下标值
	for (i = 0; i < n; i++){
		p[i].first -= i;
	}
	// 【注意】重新对x坐标进行排序
	sort(p, p + n, bmpX);
	// 获取重新处理过的x的中位数
	midX = p[n / 2].first;
	// 计算x方向的最少步数
	for (i = 0; i < n; i++){
		min += abs(p[i].first - midX);
	}
	// 对y坐标进行排序
	sort(p, p + n, bmpY);
	// 获取y的中位数
	midY = p[n / 2].second;
	// 计算y方向的最少步数
	for (i = 0; i < n; i++){
		min += abs(p[i].second - midY);
	}
	cout << min << endl;
	return 0;
}

这是个人的做题的一些想法及代码实现。已AC,如果小伙伴有更好的思路可以在评论区分享。

学无止境,加油!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值